But it's about 17 years late for that...
“This” in JavaScript
61–70 of 99 posts
Re: “This” in JavaScript
#62Earlier quoted context omitted.
Even more simply, I'd just say: 1) The keyword "this" refers to whatever is left of the dot at call-time. 2) If there's nothing to the left of the dot, then "this" is the root scope (e.g. Window). 3) A few functions change the behavior of "this"— bind , call and apply 4) The keyword "new" binds this to the object just created So if you're using the value of "this" in someFunction... thing.someFunction(); // this ===…
Exactly. Once you understand underlying concept, `this` is not complicated or weird. Small addendum to your list: "whatever is left of the dot at call-time" is a nice way to explain "base reference" concept, but be wary of non-trivial constructs: (f = thing.someFunction)(); // this references global object (function(){return thing.someFunction }())(); // this references global object eval('thing.someFunction')(); //…
But it's one of the weirdest thing in javascript, and I'd advise to forgo using 'this' in any situation that doesn't absolutely need it.
Re: “This” in JavaScript
#63Re: “This” in JavaScript
#64Re: “This” in JavaScript
#65Re: “This” in JavaScript
#66Earlier quoted context omitted.
>In JS this is just another object, like any other object. You are mistaken, "this" is not an object nor a variable, it's a keyword and behaves differently than variable scope. Consider closures for example.
And the tragic thing about "this" being a magic keyword that looks up a dynamic value in the runtime, instead of a normal lexically scoped variable like it appears, is that 90% of the time "this" is exactly the thing you want a closure to close over, so you have to do ridiculous stuff like "var self = this;". Even knowing this rule very well and being totally hard core OCO (Obsessive Compulsive ORDER, not Disorder) a…
It enables some rather elegant patterns, actually.
Re: “This” in JavaScript
#67Not mentioned are ES6's arrow functions that capture the `this` value of the enclosing context. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
I think the new arrow functions are neat, but am I the only one who never really found it a hassle to throw a .bind(this) on a function?
Re: “This” in JavaScript
#68Earlier quoted context omitted.
>In JS this is just another object, like any other object. You are mistaken, "this" is not an object nor a variable, it's a keyword and behaves differently than variable scope. Consider closures for example.
And the tragic thing about "this" being a magic keyword that looks up a dynamic value in the runtime, instead of a normal lexically scoped variable like it appears, is that 90% of the time "this" is exactly the thing you want a closure to close over, so you have to do ridiculous stuff like "var self = this;". Even knowing this rule very well and being totally hard core OCO (Obsessive Compulsive ORDER, not Disorder) a…
Re: “This” in JavaScript
#69Earlier quoted context omitted.
I think the new arrow functions are neat, but am I the only one who never really found it a hassle to throw a .bind(this) on a function?
Aside from the fact that your "never" somewhat dismisses the first however many years of web development (up to IE9) when `Function.prototype.bind` couldn't universally be expected to be supported, surely having a nice piece of syntax which is both shorter than writing out `function` and serves the purpose of binding the function in the way that, a good majority of the time, you actually want it bound, is a good thin…
Re: “This” in JavaScript
#70The "this" keyword in JavaScript is a well-known anti-pattern. Although it can be explained with a few rules, it becomes cumbersome quickly. The anti-pattern around "this" is well-known for decades. For example, SICP explicitly mentions that anti-pattern. Of course, it doesn't refer to JavaScript, but to some other old programming language which had a keyword with very similar issues. I think it's not a hyperbole to…