Do ES6 arrow functions still close over "this" even if they don't use it?

Viewed 2057

I'm trying to understand the rules of when this is lexically bound in an ES6 arrow function. Let's first look at this:

function Foo(other) {
    other.callback = () => { this.bar(); };

    this.bar = function() {
        console.log('bar called');
    };
}

When I construct a new Foo(other), a callback is set on that other object. The callback is an arrow function, and the this in the arrow function is lexically bound to the Foo instance, so the Foo won't be garbage collected even if I don't keep any other reference to the Foo around.

What happens if I do this instead?

function Foo(other) {
    other.callback = () => { };
}

Now I set the callback to a nop, and I never mention this in it. My question is: does the arrow function still lexically bind to this, keeping the Foo alive as long as other is alive, or may the Foo be garbage collected in this situation?

1 Answers
Related