r/javascript • u/MadCervantes • 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?
View all comments
Show parent comment
1
u/MadCervantes Feb 23 '18
Ooooh thanks, that helps a bunch.
To give a more visual narrative to the school example.
If I had a component in react called School, and it had a component in it called Class, and I wanted the kids in the class to go to lunch when food in the cafeteria was ready for lunch, then I'd need to bind this to cafeteria and then inside the classroom I could have a function goToLunch.this and that would execute the goToLunch function inside of the Classroom (the students being objects effected by this function) which would then use the this as the location... wait that's not right...
goToLunch would be a function inside of the cafeteria and "this" would be bound to the children objects in the Classroom, right? So when you called goToLunch in the cafeteria it would summon all the students.
Could you bind this to students in multiple classrooms, so that when you call goToLunch it would summon from all of the classrooms where you bound this?