When you write
```
for (const x of l) {
setTimeout(() => x);
}
```
we need to add a closure because the variable is meant to be block-scoped and recreated each time the block runs. We do this.
However, we also add the closure when no loop is present. This isn't necessary, because if no loop is present then each piece of code runs at most once. I changed the transform to only add a closure if a variable is referenced from within a loop.
43 lines
570 B
JavaScript
43 lines
570 B
JavaScript
function foo() {
|
|
var x = 5;
|
|
console.log(x);
|
|
|
|
{
|
|
var _x = 7;
|
|
setTimeout(function () {
|
|
return _x;
|
|
}, 0);
|
|
}
|
|
}
|
|
|
|
function bar() {
|
|
var x = 5;
|
|
console.log(x);
|
|
|
|
for (var i = 0; i < 7; i++) {
|
|
{
|
|
(function () {
|
|
var x = i;
|
|
setTimeout(function () {
|
|
return x;
|
|
}, 0);
|
|
})();
|
|
}
|
|
}
|
|
}
|
|
|
|
function baz() {
|
|
var x = 5;
|
|
console.log(x);
|
|
|
|
for (var i = 0; i < 7; i++) {
|
|
var qux = function qux(y) {
|
|
var x = y;
|
|
setTimeout(function () {
|
|
return x;
|
|
}, 0);
|
|
};
|
|
qux(i);
|
|
}
|
|
}
|