Arrow functions are one of the improvements from JS Harmony (which was ratified last night).
This commit moves them from "behind the experimental flag" to "shipping by default" in Chromium. It's a pretty big deal to people who write JS for a living.
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.
Just eliminating the words "function" and "return" doesn't make the code clearer. Anyone competent with Javascript can easily read the first form. The difference in time to read the expression may not even be measurable. If you want to argue that it makes the code neater that's fine, but the meaning is not made clearer by the substitution of sigils for words.
> Anyone competent with Javascript can easily read the first form
And anyone competent with any language implementing map can easily read:
users.map(user => user.name);
...without having to do any "skimming over" the noise. It's not a big difference, you're right, but it's the kind of little thing that, when you let it build up over time, gives you the "write-everything-in-triplicate" feel of Java 6.
Further, you have to be intimately familiar with Javascript to understand why this is necessary:
function fetchUserData(){
var self = this;
userService.fetchAllUsers()
.then(function(fetchedUsers){ self.users = fetchedUsers; });
}
or even this:
function fetchUserData(){
userService.fetchAllUsers()
.then(function(fetchedUsers){ this.users = fetchedUsers; }.bind(this));
}
as opposed to simply writing:
function fetchUserData(){
userService.fetchAllUsers()
.then(fetchedUsers => this.users = fetchedUsers);
}
As has been pointed out though, that is not the primary thing that arrow functions give you. They're really there to avoid having to manually bind functions in order to maintain execution context or use the old `var self = this; function(){/.../}` workaround.
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.
shrug it's still just code. It still does exactly the same thing as previous javascript code. Syntax changes are pretty boring IMHO, and as I said personally, I find this one much harder to read, more prone to errors, typos and confusion.
At some point things have to stop being called "syntax changes". Just because you can represent the same operation by pressing more keys doesn't mean that it's not useful to add an abstraction on top of it to ease cognitive load, otherwise we'd all be programming with switches.
I think a better argument would have been that adding something that appears to just be a shorthand for writing functions is a potential footgun to language newcomers, but I think that argument is still relatively weak.
At point have stop called . Just you represent same by more doesn't that not to an on of to cognitive , otherwise all programming switches.
My point is, removing "function" and "return" from the syntax, won't necessary easy cognitive load, at least not for senior JavaScript coders. It will save bandwidth though! :P
Javascript has first-class functions and anonymous functions, but so do lots of imperative languages, and there are plenty of other reasons to see Javascript as an imperative language. The reason we end up writing many anonymous functions is because we are using interfaces like foo.map(...) instead of interfaces like for-loops which take a block as a body (because Javascript for-loops are unbearably clumsy, e.g. iterating over properties instead of elements).
You find it harder to read because you're not used to it, that's all. Once you absorb the syntax it'll feel more native, and you'll format it in a fashion that makes it easier for you to absorb at a glance.
That's indeed pretty fun. In practice however this is much simpler:
let sqr = x => Math.pow(x, 2)
let distance = (p1, p2) => Math.sqrt(sqr(p2.x - p1.x) + sqr(p2.y - p1.y))
const zero = Object.freeze({x: 0, y: 0})
let byX = points.slice().sort((p1, p2) => p1.x - p2.x)
let byStartDistance = points.slice
.sort((p1, p2) => distance(p1, zero) - distance(p2, zero))
It's not nearly as composable, sure, but I think almost everyone would understand it much faster. The curried `distance` would also be a bit annoying to reuse in other contexts where you'd have to write `distance(p1)(p2)` instead of the idiomatic (at least now; idioms can change) `distance(p1, p2)`.
That said, the arrow function is a massive win in both examples. I've been using it for a while and never had any issue mistaking it with <= or >=. The linter would probably catch it anyway.
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:
I'm glad it's finally arriving, it been working in Firefox for quite a long time and I'm happy to see v8 implementing this ! Arrow functions are so much nicer to read and use.
Can you not see the typos possible with this? The less characters you use for something, the more error prone. There's a reason we don't write in regular expression syntax.
Who on earth would really write that? Surely you'd use the optional braces to make it clearer:
a => { return b <= 7; }
Sure you can write it without that, but only if you want to write willfully unclear code to annoy people, like writing an entire function body on one line. Just because you can do that doesn't mean that functions are inherently unclear.
Just make it a => (a <= 7), that is perfectly fine for me. (I replaced the b with an a, otherwise it looks pretty pointless or maybe a strange closure hack.)
If the function `foo` is defined as taking a function as its first argument (or even more precisely, a function which takes a number and returns a number), and you try to give it a boolean, then the static type checker will yell at you, and you'll just as easily find out as that fnuction typo.
Spend a half decade writing Perl and you'll develop an eye for minute details like that.
People aren't kidding when they say Perl looks like line noise, but you get used to it and can find your way through even a dizzying regular expression with relative ease over time.
You can get used to it, but if it takes 5 years to get reliable at parsing line noise then that many more programmers are struggling with it at any given time. Why shouldn't languages try to help rather than hinder for 5 years?
C++, by way of example, is extremely complicated and wickedly unforgiving. JavaScript, even at its most surreal, is nowhere near that.
Suggesting that things like => are "too confusing" for JavaScript is to suggest that people that write JavaScript are too dumb to handle it. Is that what you're implying here?
Some programmers write in one language, but many write in several. Having the same shorthand available in JavaScript as in C# is a big deal, it makes things feel better. Being able to apply what you've learned in one domain directly to another is more efficient than having some ridiculously quirky JavaScript-only way of doing something.
That some people will abuse it or be confused is a problem that can be overcome with a little bit of learning. It's not a big deal.
What the heck is the second one supposed to mean? At first it looks a bit like low <= x <= high but with one comparison operator in the wrong direction. I guess it will first compare a and b evaluating to a boolean value, then implicitly convert this result to an integer and finally compare that to 7 again yielding a boolean value. If you write code like that confusion with the arrow notation is one of your smaller problems.
EDIT: Now I got your point - you could accidentally assign a boolean value to a variable when you intended to create a function. One word - compile time type checking. Okay, four, Übersetzungszeittypprüfung in German.
This is a very wrong way of reasoning about probability. The chance that a coin will at some point land on heads does not decrease with the number of flips.
mmmm, I haven't considered this case... I personally preferred the Coffeescript version like this: a->b<=7 which even without spaces is clearer but maybe they had something incompatible with it.
Do the stack traces for native arrow functions show up in the stack trace as anonymous functions or is only the parent function registered in the stack trace.
Anyone have any idea when destructuring assignment is going to make it into v8?
That is one of the features my colleagues most cite as justification for using babel.js (I wouldn't mind destructuring assigment either). I would love to be able to avoid transpilation since it adds overhead from making out our toolchain more complex and makes debugging more difficult because stack traces and line numbers don't necessarily match up.
You can tell by the number of CLs that a lot of work has been done already but there are a few more still in progress. It's probably still a few weeks out.
Disclaimer: I don't speak for the V8 team, I'm just an interested onlooker.
edit: after following 4 links I finally found the bug report about it
https://code.google.com/p/v8/issues/detail?id=2700