back

by padolsey·14y ago·view on hn ↗
It's not something that can be resolved by simply removing `Function.apply` or the `new` operator. The way that the `this` keyword works is an integral of how JS works. There's nothing stopping you from using `Function.prototype.bind` or defining an event mechanism that retains its assigner's `this` value. That's what's so great about JS -- you have this level of control.

As far as events go, having `this` set to the element receiving the event is, IMO, entirely logical, and has been a common pattern way before jQuery came into existence. You can always access the target as `event.target` if you'd prefer.

If you have to look up "what is this set to in this callback" constantly, then you just need to learn the API/language.

1 comments
> The way that the `this` keyword works is an integral of how JS works.

Says who? Antiquated API design by the W3C (which by the way had no reference implementation) shouldn't be the defining factor in how a language works. 'this' and 'new' were brought over to JS so people who were familiar with Java would 'get it'. JS was designed around different ideas before it was mutilated for the sake of superficial familiarity.

> If you have to look up "what is this set to in this callback" constantly, then you just need to learn the API/language.

I assert that good API design requires you to not have to look up things like this. Things that could be unambiguous. Usage of 'this' introduces ambiguity. Not to mention that in an asynchronous programming environment, if you're doing anything non-trivial and you're not copy-pasting $.ajax() code snippets, you want to have named references to things so that closures written inside the scope have access to whatever interesting thing you're working with.

var that = this is a huge indicator that API design is wrong, that this is an abomination and that people generally aren't thinking critically about the code they're writing.

Downvotes? What on earth for? Oh man you front-end guys really don't get it. If you disagree with my sentiments, that's fine. If you don't understand what I'm talking about, you're not a real programmer.
Check your facts.

The standards body for JavaScript is Ecma International -- hence JavaScript being officially known and standardized as "ECMAScript".

Additionally, JavaScript began at Netscape in 1995 two years before it was a published standard.

See Wikipedia:

- http://en.wikipedia.org/wiki/ECMAScript

- http://en.wikipedia.org/wiki/Ecma_International

The W3C had nothing to do with the design of JavaScript or even DOM 0.
No actually the down-votes are because you don't know what you're talking about --and are arrogant about it on top.