Live data from Hacker News

“This” in JavaScript

bjorn.tipling.com

81–90 of 99 posts

Re: “This” in JavaScript

#81

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…

Sure, my .bind was a little insensitive to those who had to suffer through ES3, but honestly I thought we were pretty much ignoring the 90s.

Re: “This” in JavaScript

#82
post #72

Earlier quoted context omitted.

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

Not sure where you got this from. JavaScript does use lexical scoping for almost everything, except for "this".

[deleted]

Re: “This” in JavaScript

#83
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?

I think it adds too much line noise, especially if people start nesting callbacks.

Re: “This” in JavaScript

#84
post #62
post #57

Earlier quoted context omitted.

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.

So basically most open source JS code out there is completely wrong?

Re: “This” in JavaScript

#85
post #76

I've used this gist: https://gist.github.com/erichummel/2595398 a few times in interviews to see just how much of a javascript whiz candidates were. The basic gist of the interview question is "what is output to the console when this code is run?" It's obviously intentionally obfuscated, so even if the candidate got it wrong (most did), subsequently talking through the solution told me a lot about how good a candidat…

Does `setTimeout` have a default value for the time?

setTimeout with no timeout argument simply schedules the provided function for when the interpreter is next idle. Underscore.js wraps a similar unqualified call in _.defer (last i checked, anyway; they might now provide it with a very small number or 0 for the timeout).

Edit: in both cases the result is the same in most browsers.

Re: “This” in JavaScript

#86
post #72

Earlier quoted context omitted.

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

Not sure where you got this from. JavaScript does use lexical scoping for almost everything, except for "this".

Yeah, that's what I meant, but it was before caffeine this morning and now its too late to edit.

Re: “This” in JavaScript

#87

Earlier quoted context omitted.

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…

Sure, my .bind was a little insensitive to those who had to suffer through ES3, but honestly I thought we were pretty much ignoring the 90s.

You say "the 90s", I say "2011". Potayto, potahto!

Re: “This” in JavaScript

#88

The author critically misunderstands the way that `this` gets set, and in doing so has developed a significantly overcomplicated mental model. This is exemplified in the statement: You can use this in any function on an object to refer to other properties on that object. This is not the same as an instance created with new. ... Note, there was no use of new , no Object.create and no function called to create obj. Thi…

> The author critically misunderstands the way that `this` gets set... I don't actually. (I'm the author) I know that the this key word is executed at run time to figure out what this is. It is true there are simpler ways to explain this and this blog post isn't that. I link to such a post at the bottom of my own. These are just some examples. There's also more to that blog post than what everyone is discussing, like…

Phrasing like "they this keyword is executed at run time" doesn't exactly help your case...

Re: “This” in JavaScript

#89
post #27

Earlier quoted context omitted.

The article was a nice refresher about 'this' subtleties, but it put too much work into contrasting it against Java's this, instead of just laying out the actual JS rules. To me, 'this' is JS is a lot more self explanatory than this is Java. And your article doesn't make that apparent. In JS this is just another object, like any other object. It follows the exact same scoping rules as every other object.

>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.

The way I think about it ('this' being like any other object), closures are just supplied a default 'this' object (window, in the case of web), and instead of providing a this argument via the argument list, you can choose it with the apply/call/bind functions.

Re: “This” in JavaScript

#90
post #62

Earlier quoted context omitted.

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.

So basically most open source JS code out there is completely wrong?

Suboptimal might be a better word. And it should be no surprise that most open source anything is suboptimal.
Post reply on HN