Let transform breaks when function closes over variables declared after function is called #168

Open
nene opened this Issue Aug 19, 2016 · 0 comments

Projects

None yet

1 participant

@nene
Collaborator
nene commented Aug 19, 2016

Running the following code will log undefined:

fn();
var foo = "hello";

function fn() {
    console.log(foo);
}

but after transforming it to let, the following will give "ReferenceError: foo is not defined":

fn();
let foo = "hello";

function fn() {
    console.log(foo);
}

The cause is that in first case the foo and fn variables get both hoisted, and when the function is called it can reference the variable.

But with let the variable foo is not hoisted, and when the function is called the variable doesn't exist yet, so we'll get ReferenceError.

@nene nene added the bug label Aug 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment