HaxeFoundation / HaxeFoundation/haxe
unnecessary wrapping of captured vars in loop
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
*extracted from #9624*
I'm pretty sure we have an issue about this somewhere, but i couldn't find it:
```haxe
function main() {
var x = 0;
var i = 0;
while (i < 5) {
var j = x;
function capture() {
trace(j);
}
capture();
}
}
```
Generates the following ES5 (but NOT ES6):
```js
function Main_main() {
var x = 0;
var i = 0;
while(i < 5) {
var j = [x];
var capture = (function(j) {
return function() {
console.log("src/Main.hx:7:",j[0]);
};
})(j);
capture();
}
}
```
Changing `while` to `if` removes the wrapping, so it's specific to loops, it seems. Also it's not JS-specific, result is the same also for Flash and Python, but not for C# and C++, so this seems dependant on platform settings.
Contributor guide
Assessment
This issue has not been assessed yet.