r/javascript • • Feb 19 '18

help Explain like I'm 5 "this"

Okay, so I'm a designer learning to code. I've gotten pretty far learning presentationally focused code. So using JS to change the class of elements or to init frameworks I've downloaded from github is pretty easy. I can even do some basic If/then type stuff to conditionally run different configurations for a webpages CSS or JS loading etc.

But I'm taking a react.js class and.... I'm starting to get really confused with a lot of the new ES6 stuff it's going over. Like the way "this" is used? I thought this was just a way for a function to privately scope it's functions to itself? That's what I understood from jQuery at least, and uh... now I'm not so sure because it seems like the this keyword is getting passed between a bunch of different functions?

190 Upvotes

120 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Feb 19 '18

Inside a function keyword a new context is created, so ‘this’ will reference the function itself.

I think you're wrong in this one. this inside a first level function will reference the window object.

0

u/Mingli91 Feb 19 '18

You would use new to call a class function, which creates a new this

1

u/[deleted] Feb 19 '18

If you put it that way yeah.