3 ms·
I understand your sentiment in general, but disagree in this particular case. I've been writing JavaScript for years in various projects, companies, partnership
by rybosome 13y ago
I understand your sentiment in general, but disagree in this particular case. I've been writing JavaScript for years in various projects, companies, partnerships, etc. If I had a nickel for every time I saw...
asyncOperation(_.bind(function(x) { this.x += x; }, this));
or...
asyncOperation(goog.bind(function(x) { this.x += x; }, this));
or...
asyncOperation(function(x) { this.x += x; }.bind(this));
or...
asyncOperation($.proxy(function(x) { this.x += x; }, this));
or even...
var self = this;
asyncOperation(function(x) { self.x += x; });
...then I'd have a lot of nickels. Binding a function to the current scope is _insanely_ common - adding syntax specifically for it makes sense. I would argue that it reduces the cognitive overhead of the language, since rather than needing to read another method call, examine arguments, etc. I can simply translate a 2-character symbol into the meaning "outer-context-bound function". There's a lot more bloat in the above examples than there is in this:
asyncOperation((x) => { this.x += x; });
This is like arguing that the '+' operator bloats the language, when we could all just be using Number(17).plus(Number(43));
- EGreg 13y agoWell, in this particular case I agree. If an operation is very commonly used, and/or it can allow major compiler/interpreter optimizations by doing it at the language-level instead of a library level, then you should put it into the language. But this is not the case for SO MANY constructs. Especially in a language like PHP, where they've recently added even more stuff that very few people would care about.
- rybosome 13y agoThat's fair. If language developers were willing to deprecate more aggressively, it wouldn't be such a problem...but every hacked-up, shoehorned feature represents a major investment that must be tended to for years.