ES6 arrow function and lexical scope inside a function [duplicate]

When you define a and d, the value of this is the top-level window object, since you’re not in the context of some other object, and this gets saved in the arrow functions. window.name is an empty string, so that’s what you see when you call a.func() and d.func().

Arrow functions should generally not be used as method functions because they don’t get access to the object they’re called through. Use them when you want to preserve the binding of this from where you created them (just like other closure variables).

Leave a Comment

tech