Skip to content

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

@nene

Description

@nene

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions