<HTML>
<body>
</body>
<script>
function makeArmy() {
let shooters = [];
let i = 0;
while (i < 10) {
let j = i; // so that each array elements has separated
//lexical environments which is generated by each run of while{}block
let shooter = function() { // shooter function
alert( j ); // should show its number
};
shooters.push(shooter);
i++;
}
return shooters;
}
let army = makeArmy();
army[0](); // the shooter number 0 shows 10
army[5](); // and number 5 also outputs 10...
// ... all shooters show 10 instead of their 0, 1, 2, 3...
</script>
</HTML>
<!--
I beat the solution this time again!!baaaam!!
-->
<body>
</body>
<script>
function makeArmy() {
let shooters = [];
let i = 0;
while (i < 10) {
let j = i; // so that each array elements has separated
//lexical environments which is generated by each run of while{}block
let shooter = function() { // shooter function
alert( j ); // should show its number
};
shooters.push(shooter);
i++;
}
return shooters;
}
let army = makeArmy();
army[0](); // the shooter number 0 shows 10
army[5](); // and number 5 also outputs 10...
// ... all shooters show 10 instead of their 0, 1, 2, 3...
</script>
</HTML>
<!--
I beat the solution this time again!!baaaam!!
-->
댓글
댓글 쓰기