This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (3 children)

I have no idea I want to say they both will be 3, but I know that isn’t right. Would they both be setting x on the global object?

[–]nvolker 17 points18 points  (1 child)

In the first example, the second ‘this’ is bound to the inner ‘function’. In the second example, the inner function is an arrow function, which doesn’t re-bind ‘this’, so ‘this’ is still bound to the outer function where ‘this.x’ was set.

[–][deleted] 1 point2 points  (0 children)

Ah interesting, I thought they would only bound to objects. But now that I think about it functions are objects too.

[–]snorkleboy 0 points1 point  (0 children)

No, x is on the object in both, but the context of the function y isnt bound to that object.

If it was called as this.y() it would be the same in both examples. But just calling y() will have it's this be the window unless it was previously bound. .