Hacker News new | ask | show | jobs
by _mtr 4207 days ago
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...

2 comments

TypeScript has these and they're marvelous. They make writing functional style code very enjoyable and not having to capture `this` yourself is alone justification for this developer to never go back to pure JavaScript.
Might you go back when ES6 is widely supported by browsers / Node? I don't mind either way, but since the parent comment mentioned that arrow functions were coming to JS, it was interesting that you specifically said 'never', rather than 'until ES6'.
Yeah I realize that may not have been the clearest statement, thanks. While ES6 is nice, it just doesn't have the elegance and utility of some of the other TypeScript things like generics and the interfacing system.

Also, it will probably be a long time until it can be widely used, given the existence of certain browsers and versions.

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

As someone who's spent a lot of time writing CoffeeScript, I think ES6's fat arrows are a great thing, though I have my usual concerns about their confusing the hell out of people who may well have been writing JavaScript for a long time but in environments where they don't necessarily get exposed to new and exciting stuff. But hey, that's progress, I guess.

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.
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!
I think it adds too much line noise, especially if people start nesting callbacks.
Not supported in ES3, IIRC.