1、阅读程序,判断输出结果
/**
* @param {number} n
* @return {Function} counter
*/
var createCounter = function(n) {
let k = n - 1
return function() {
k = k + 1
return k
};
};
/**
* const counter = createCounter(10)
* counter()
* counter()
* counter()
*/
2、阅读程序,判断输出结果
/**
* @param {number} millis
*/
async function sleep(millis) {
let p = new Promise((succFunction, failFunction) => {
//耗时操作
setTimeout(function () {
succFunction();
}, millis);
})
return p
}
/**
* let t = Date.now()
* sleep(100).then(() => console.log(Date.now() - t))
*/
3、阅读程序,判断输出结果
Array.prototype.last = function () {
return this.length == 0 ? -1 : this[this.length - 1]
};
/**
* const arr = [1, 2, 3];
* arr.last();
*/
4、阅读程序,判断输出结果
/**
* @param {Function[]} functions
* @return {Function}
*/
var compose = function (functions) {
return function (x) {
for (let i = 0; i < functions.length; i++) {
x = functions[functions.length - 1 - i](x)
}
return x
}
};
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4)
*/
5、阅读程序,判断输出结果
/**
* @param {number[]} arr
* @param {Function} fn
* @return {number[]}
*/
var filter = function (arr, fn) {
let result_arr = []
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i], i)) {
result_arr.push(arr[i])
}
}
return result_arr;
};
/**
输入:arr = [0,10,20,30], fn = function greaterThan10(n) { return n > 10; }
*/
6、阅读程序,判断输出结果
/**
* @return {Generator<number>}
*/
var fibGenerator = function* () {
let a = 0, b = 1
while (true) {
yield a;
[a, b] = [b, a + b]
}
};
/**
* const gen = fibGenerator();
gen.next().value;
gen.next().value;
gen.next().value;
gen.next().value;
gen.next().value;
*/
7、阅读程序,判断输出结果
/**
* @param {integer} init
* @return { increment: Function, decrement: Function, reset: Function }
*/
var createCounter = function (init) {
let defaultValue = init
return {
increment() {
return init = init + 1
},
decrement() {
return init = init - 1
},
reset() {
return init = defaultValue
}
}
};
/**
* const counter = createCounter(5)
* counter.increment();
* counter.reset();
* counter.decrement();
*/
====================华丽分隔线=======================
1、答案(考点闭包)
10
11
12
2、答案(考点Promise)
100
3、答案(考点prototype)
3
4、答案(考点复合函数)
9
5、答案(考点函数形参与实参数量可以不一致。形参多余实参,多出的被拦截而被忽略;形参少于实参,缺少的被判断为undefined(不存在),但都不报错。)
[20,30]
6、答案(考点生成器函数function*和yield)
0
1
1
2
3
7、答案(考点对象中可以添加函数)
6
5
4
网友评论