eocanha
By: a guest | Apr 2nd, 2009 | Syntax:
JavaScript | Size: 0.52 KB | Hits: 333 | Expires: Never
<html>
<body>
<script type="application/javascript;version=1.7">
let funs = {};
let msg = "";
for (let i = 0; i < 10; i++) {
funs[i] = function() {
msg += "i = " + i + "\n";
}
}
for (let i = 0; i < 10; i++) funs[i]();
alert("Without redeclaration: \n"+msg);
msg = "";
for (let i = 0; i < 10; i++) {
let idx = i;
funs[i] = function() {
msg += "i = " + idx + "\n";
}
}
for (let i = 0; i < 10; i++) funs[i]();
alert("With redeclaration: \n"+msg);
</script>
</body>
</html>