試圖通過i
進入封閉來使它局部避免i
關閉問題基本維持在2爲什麼我不能傳遞的參數在起作用for循環
var myFunctions = {};
for (var i = 0; i < 3; i++) { // let's create 3 functions
myFunctions[i] = function(i) { // and store them in myFunctions
console.log("My value: " + i); // each should log its value.
};
}
for (var j = 0; j < 3; j++) {
myFunctions[j](); // and now let's run each one to see
}
// > "My value: undefined
// > "My value: undefined
// > "My value: undefined
那麼當你調用函數,你** **不傳遞參數。 – Pointy
我是一個白癡:) – js2015
調用它像'myFunctions [j](j);' – Redu