8000 An issue about let statement in typescript. · Issue #6356 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
An issue about let statement in typescript. #6356
Closed
@arliang

Description

@arliang

http://www.typescriptlang.org/Playground#src=for(let%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%7B%0D%0A%09setTimeout(function()%7B%0D%0A%09%09console.log(i)%3B%0D%0A%09%7D)%3B%0D%0A%7D

for(let i = 0; i < 3; i++){
    setTimeout(function(){
        console.log(i);
    });
}
for (var i = 0; i < 3; i++) {
    setTimeout(function () {
        console.log(i);
    });
}

In es6, the first example outputs 0 1 2
But the JS which translated by TS outputs 3 3 3

It would act like es6 if wrapped with (function(i){ /* */ })(i)

for (var i = 0; i < 10; i++) {
    (function(i){
        setTimeout(function () {
            console.log(i);
        });
    })(i);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0