r/javaScriptStudyGroup • • Jan 14 '18

"this" keyword confuses me

Hey all. I have done minimal javascript work in the past and have often run into weird situations with the word this. Sometimes, the compiler tells me to do .bind(this) and other weird things that I just can never really grasp. What is "this"? Why do we use it and how do I use it correctly?

3 Upvotes

4 comments sorted by

2

u/owlanalogies Jan 15 '18

Hey there - yeah, 'this' can be a little confusing; it's related to the scope of the function being called, so if the scope is global, "this" will be the browser window, etc... If the scope is local, then "this" will refer to the variable the function is being set to. This is an article I found really helpful when trying to learn more - hope it helps! http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/

2

u/Major_Fifth Jan 15 '18

Thanks! It does help a lot. Now I just need to practice using it.

2

u/owlanalogies Jan 16 '18

I'm so glad that helped! Yeah, repetition is really the only way to get it all to stick. I was just searching around a bit, and I unfortunately can't find any good practice sets that focus just on the "this" keyword. I don't know what your level is, but if you're looking for a nice overview on the language, there is a ton of good information in the JS programs at codeacademy.com and freecodecamp.com.