http://wiki.ecmascript.org/doku.php?id=harmony:let
Given the following code:
var a = [];
for (let i = 0; i < 3; i++) {
a.push(function () { return i; });
}
print(a[0]()); // 0 or 3?
I would expect this “desugaring”:
var a = [];
(function() {
var i;
for (i = 0; i < 3; i++) {
(function() {
a.push(function () { return i; });
}());
}
}());
print(a[0]()); // 3
I remember that this has been previously discussed, but can't find the thread.
For me, the above desugaring is easier to understand than the lambda coding.
Compare: With Java, you have to declare
final int finalI = i;
inside a for loop if you want to refer to i from an inner class. Similarly, if
you want the result "0", you would need to write:
var a = [];
(function() {
var i;
for (i = 0; i < 3; i++) {
(function() {
var iCopy = i;
a.push(function () { return iCopy; });
}());
}
}());
print(a[0]()); // 0
Axel
--
Dr. Axel Rauschmayer
[email protected]
twitter.com/rauschma
home: rauschma.de
blog: 2ality.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss