Live data from Hacker News

“This” in JavaScript

bjorn.tipling.com

61–70 of 99 posts

Re: “This” in JavaScript

#61
Maybe, rather than overriding the C++ / Java keyword with a different meaning, this should have been called something completely else in JavaScript. I think context would have been more descriptive.

But it's about 17 years late for that...

Re: “This” in JavaScript

#62
post #57
post #31

Earlier 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')(); //…

I agree it's a simple concept.

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

#66
post #27

Earlier 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…

I'd say I use `this` outside of lexical context at least as much as I do within, and I rely on this's behaviour a lot when developing a DSL.

It enables some rather elegant patterns, actually.

Re: “This” in JavaScript

#67
post #2

Not 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?

Not supported in ES3, IIRC.

Re: “This” in JavaScript

#68
post #27

Earlier 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…

I agree. The fact that you have to write library code defensively to still work depending on how your method's call, or that you have to use an awkward bind to preserve the `this` context of a method is so annoying and error-prone to me.

Re: “This” in JavaScript

#69

Earlier 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…

I agree. Fat arrow is a massive improvement. Not just for its extremely logical handling of "this", which 95% of the time is the behavior I want, but also because it eliminates a massive amount of syntax noise when programming functionally.

Re: “This” in JavaScript

#70
post #46

The "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…

What? No, it's not an anti-pattern. It's about understanding functional scoping, JavaScript doesn't use lexical scoping.
Post reply on HN