JavaScript: Clarifying the Keyword ‘this’
Check out the discussion about this blog post on HN:
news.ycombinator.com/item?id=4985853
The keyword ‘this’ holds a value and is bound to another object. It can be one of the most confusing subjects in JavaScript. It doesn’t have to be if you follow this simple rule:
The keyword ‘this’ is bound dynamically to the object found to the left of the ‘.’ at call time.
Call time is when a set of parenthesis () begins the execution of our function. In our case ”person” is to the left of the ‘”.’” at call time. So in the example above, ‘this’ refers to our object person.
There are three exceptions to the above.
1) When there is no ‘.’ the keyword ‘this’ is bound to the global object window.
2) When you use call and apply, you get to decide what ‘this’ is bound to.
3) When you use the keyword new to create a new instance from a constructor, the keyword ‘this’ refers to the newly generated instance.
Special shout out to Marcus who’s a software engineer at Twitter and curriculum designer at Catalyst Class for teaching me how simple the keyword ‘this’ is. All credit goes to him.
19 Notes/ Hide
-
theinternauts likes this
-
zachshallbetter likes this
-
tumadman likes this
-
divineflame likes this
-
crowchick likes this
-
pdxiii reblogged this from henrycode
-
pdxiii likes this
-
daftken likes this
-
keogh likes this
-
nicksh likes this
-
arahmanshah reblogged this from henrycode
-
arahmanshah likes this
-
oceanhere likes this
-
hassan-khan likes this
-
scotth likes this
-
codreamers likes this
-
techphilosophy likes this
-
henrycode posted this

