javascript-learner/6.3variablescope/t.js
2024-06-22 23:54:56 +02:00

40 lines
857 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function sum(a){
return function(b){
return a + b;
}
}
console.log(sum(1)(2));
console.log(sum(5)(-1));
function byField(Field){
return (a,b) => a[Field] > b[Field]? 1: -1;
}
function makeArmy() {
let shooters = [];
let i = 0;
while (i < 10) {
let id = i;
let shooter = function() { // 创建一个 shooter 函数,
console.log( id ); // 应该显示其编号
};
shooters.push(shooter); // 将此 shooter 函数添加到数组中
i++;
}
// ……返回 shooters 数组
return shooters;
}
let army = makeArmy();
// ……所有的 shooter 显示的都是 10而不是它们的编号 0, 1, 2, 3...
army[0](); // 编号为 0 的 shooter 显示的是 10
army[1](); // 编号为 1 的 shooter 显示的是 10
army[2](); // 10其他的也是这样。