Let transform breaks when function closes over variables declared after function is called
nene opened this issue · comments
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.