This question already has an answer here:
I'm trying out ES6 and want to include a property inside my function like so
var person = {
name: "jason",
shout: () => console.log("my name is ", this.name)
}
person.shout() // Should print out my name is jason
However, when I run this code console only logs my name is
. What am I doing wrong?
Short answer:
this
points at the nearest boundthis
- in the code providedthis
is found in the enclosing scope.Longer answer: Arrow functions
bind theirdo not havethis
when they are createdthis
,arguments
or other special names bound at all - when the object is being created the namethis
is found in the enclosing scope, not theperson
object. You can see this more clearly by moving the declaration:And even more clear when translated into a vague approximation of the arrow syntax in ES5:
In both cases,
this
(for the shout function) points to the same scope asperson
is defined in, not the new scope that the function is attached to when it is added to theperson
object.You cannot make arrow functions work that way, but, as @kamituel points out in his answer, you can take advantage of the shorter method declaration pattern in ES6 to get similar space savings:
Here the value of this inside of the function is determined by where the arrow function is defined not where it is used.
So
this
refers to global/window object if not wrapped in other namespaceAgreed with @Sean Vieira - in this case
this
is bound to the global object (or, as pointed out in the comment, more generally to an enclosing scope).If you want to have a shorter syntax, there is another option - enhanced object literals support short syntax for property functions.
this
will be bound as you expect there. Seeshout3()
:The accepted answer is excellent, concise, and clear but i will elaborate a little on what Sean Vieira said:
Because the arrow function doesn't have a "this", it uses the parent's "this". "this" always points to the parent, and the parent of the person object is Window (if you're in a browser).
To prove it run this in your console:
You'll get the Window object.
I find this to be a really helpful way to think about it. It isn't quite the full story, as what the "this" of an object literal is is another discussion.
The problem is that (MDN)
Therefore, the value of
this
in that function will be the value ofthis
where you create the object literal. Probably, that will bewindow
in non-strict mode andundefined
in strict mode.To fix it, you should use a normal function: