3 ms·
An entire book is dedicated to "this", when it's only "the stuff on the left side of the dot" (it changed my life when i understood that)
by 131hn 10y ago
An entire book is dedicated to "this", when it's only "the stuff on the left side of the dot" (it changed my life when i understood that)
- kabes 10y agoIt's just one chapter and the rest of the book is about prototypes and inheritance. It's also not just 'the stuff on the left side of the dot'. using strict mode, call/apply, the new keyword and arrow functions have influence on what `this` is. And they are applied in a certain order of precedence you need to know.
- Udik 10y agoI found it pedantic as well. 'This' really is just the object to the left of the dot, unless 'new' is used to invoke the function with a brand new 'this' object. Functions called without a 'left-of-the-dot' argument, not having a 'this' injected, just see that of the global scope (Window). Call and apply allow to explicitly inject the 'this' object. That pretty much sums up at least 80% of the chapter.
- rashkov 10y agoAs kabes pointed out above, there are more exceptions than the ones you mention. Don't forget bind, call/apply, strict mode, and arrow functions. "this" is just a reference to the function context. Function context has a strong definition in programming languages. Saying that it's the stuff to the "left-of-dot" modulo a bunch of exceptions is sort of missing the forest for the trees, in my opinion. Left-of-dot is a great way to introduce the concept but function context is so much more than just that.
- grwgreg 10y agonot if you invoke it like this (1,someobj.somefn)()