The actual functionality of the first is swallowed in visual noise (function, return). Yes, it's something new to learn for Javascript programmers, but it's useful and makes the meaning of code clearer.
That's not even touching on the this-binding element, which is also incredibly useful in common Javascript code.
I've been doing ES6 for several months now. Not once have I mistook `=>` for `>=`. Not once have I made a typo when I wanted an arrow function.
Of course any syntax can be abused in such a way to create ugly code. But if I compare
[1, 2, 3].map( a => a * a ); // [1, 4, 9]
to
[1, 2, 3].map(function(a) { return a * a; });
I prefer the former. It's more concise, and strips off a lot of visual noise, and I think it's more readable (the intent isn't wrapped in lots of boilerplate).
That doesn't mean one can't write horrible code with the arrow form. Of course you can. Furthermore, it's not necessarily something you should use all the time, because it's not completely analogous to a normal `function` because it binds `this` lexically. Which means `function` isn't going away any time soon, especially if you need to bind them dynamically.
In short: use the best (syntactic) tool for the job. There are use cases where the arrow makes sense semantically, and there are cases where it doesn't. Use as appropriate, as one should always do.
Actually that code example is hard to read because your variables are called 'foo' and 'a' and you've chosen not to include white space around your operators.
The lambda function is a foundational construct in computer science (1936), and it is often written using an arrow. Hipster fluff it is not.
You are trolling, but arrow functions (unlike those defined with the function keyword) also bind this to that of the enclosing scope. The results in more intuitive behavior in most cases and less explicit bindings.
Your parent post skimmed over an important detail, it's not just a new syntax, it's new type of anonymous function with a new type of scope. It's more like:
gloriousduke|10 years ago
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
eueueu|10 years ago
phs2501|10 years ago
That's not even touching on the this-binding element, which is also incredibly useful in common Javascript code.
photokandy|10 years ago
Of course any syntax can be abused in such a way to create ugly code. But if I compare
to I prefer the former. It's more concise, and strips off a lot of visual noise, and I think it's more readable (the intent isn't wrapped in lots of boilerplate).That doesn't mean one can't write horrible code with the arrow form. Of course you can. Furthermore, it's not necessarily something you should use all the time, because it's not completely analogous to a normal `function` because it binds `this` lexically. Which means `function` isn't going away any time soon, especially if you need to bind them dynamically.
In short: use the best (syntactic) tool for the job. There are use cases where the arrow makes sense semantically, and there are cases where it doesn't. Use as appropriate, as one should always do.
jahewson|10 years ago
The lambda function is a foundational construct in computer science (1936), and it is often written using an arrow. Hipster fluff it is not.
gfodor|10 years ago
spion|10 years ago
ajanuary|10 years ago
jonesetc|10 years ago
function(arg) {statement;}.bind(this)
towelguy|10 years ago