r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

61

u/[deleted] May 28 '19

[removed] — view removed comment

28

u/[deleted] May 28 '19

more importantly, understanding the different contexts of this:

this in the global space => window

this in a function => function context

this in a method of an object => the object

this with call/apply/bind => whatever you want it to be

edit for ES6: this is not bound in arrow functions, so the value bound outside the arrow function will be found and used instead.

0

u/[deleted] May 28 '19

[deleted]

1

u/[deleted] May 28 '19

can you give me an example of how running

let o = { f: function() { console.log(this); } };

o.f();

prints out anything except the Object o?

Also, to your first point, yes you can change the context at the call point with call/apply/bind, however, am I wrong to say that this applies to the closure created by the function in non call/apply/bind cases?

→ More replies (2)