Javascript
1. 請優(yōu)化這段代碼 公荧,提高可讀性
function async_add (m, n, callback) {
setTimeout(callback.bind(null, m+n), 500);
}
async_add(1, 2, function (res) {
console.log(res);
async_add(res, 3, function (res) {
console.log(res);
async_add(res, 5, function (res) {
console.log(res);
async_add(res, 10, function (res) {
console.log(res);
async_add(res, 25, function (res) {
console.log(res);
async_add(res, 37, function (res) {
console.log(res);
async_add(res, 48, function (res) {
console.log(res);
})
})
});
});
});
});
});
2. 請給出這段代碼 的運行結果休涤,并解釋為什么
class Animal {
constructor(pos) {
this.pos = pos;
}
}
class Whale extends Animal {
constructor(pos) {
super(pos + 1);
}
swim() {
this.pos <<= 1;
setTimeout(function timer() {
this.pos <<= 1;
this.pos < 8 && setTimeout(timer, 0);
console.log(this.pos);
}, 0);
console.log(this.pos);
}
}
var pos = 1;
const whale = new Whale(pos);
whale.swim();
3. 請實現(xiàn)下段代碼
/**
interface Array<T> {
//flatMap :: (t -> [u]) -> [t] -> [u]
flatMap<U>(f: (item: T) => U[]): U[];
}
*/
Array.prototype.flatMap = function (f) {
// 實現(xiàn)這個函數(shù)咏连,使得下面的測試用例能夠正確輸出
throw new Error('Not implemented yet!')
}
/**
* 測試用例
*/
const arr1 = [1, 2, 3], arr2 = ['a', 'b', 'c']
const CartesianProduct = arr1.flatMap(x => arr2.flatMap(y => [[x, y]]))
//result => [[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c'], [3, 'a'], [3, 'b'], [3, 'c']]
const flattenArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]].flatMap(x => x)
//result => [1, 2, 3, 4, 5, 6, 7, 8, 9]
html/css
請根據(jù)此效果圖,兼容現(xiàn)代瀏覽器卦羡,盡可能還原噪馏。 (不限框架,工具绿饵,預編譯語言)
兼容性要求:Chrome / Firefox / IE 11 / Edge