我认为我理解了变量存在于声明它们的函数之外的原因,因为你正在返回另一个函数:
myFunction = function() {
var closure = 'closure scope'
return function() {
return closure;
}
}
A = myFunction(); // myFunction returns a function, not a value
B = A(); // A is a function, which when run, returns:
console.log(B); // 'closure scope'
现在的写法中,调用A()就像是一个getter。
问:我该如何编写myFunction,以便调用A(123)成为一个setter?