这是一个示波器问题。 预期结果应为:
arr[0] = function(x){return x + 0},
arr[1] = function(x){return x + 1},...
由于范围问题,它变为arr[0] = function(x) {return x + i}
其中i
等于y
我不明白为什么i
中的y
等于function(x)
这是我对代码的理解,假设y = 6,x = 4?
//i,y,x are not known here
function createArrayOfFunctions(y) {
//i is known here, but undefined
//y is known here, which is 6
//x is not known here
var arr = [];
for(var i = 0; i<y; i++) {
// i is known here it has a value,1,2,.....,6 right?
// y is known here it has a value of 6
// x is not known here
arr[i] = function(x) {
//x is known here, it has a value of 4
//y is known here, it has a value of 6
//I dont get why i has the same value as y here?
return x + i; }
}
return arr; }