r/javaScriptStudyGroup • u/Major_Fifth • 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
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/