by feld · 11y ago · 0 comments
edit: after following 4 links I finally found the bug report about it
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.
var square = function (a) { return a * a;}
You can write: var square = a => a * a;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
// foo takes a function, and a boolean.
// foo(a=>7, a>=2);
Really? That's easy to read is it? Eugh count me out. list.map(function(x) { return x * 2; });
verses list.map(x => x * 2);
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.
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.
The lambda function is a foundational construct in computer science (1936), and it is often written using an arrow. Hipster fluff it is not.
let sqr = x => Math.pow(x, 2)
let property = name => obj => obj[name]
let distance = p1 => p2 => Math.sqrt(sqr(p2.x - p1.x) + sqr(p2.y - p1.y))
let byComparing = f => (x, y) => {
let fx = f(x), fy = f(y)
fx < fy ? -1 : fx > fy ? 1 : 0
}
let points = [{x: 1, y: 2}, ...]
let byX = points.slice().sort(byComparing(property('x'))
let byStartDistance = points.slice().sort(byComparing(distance({x: 0, y: 0})) 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.
function(arg) {statement;}.bind(this)
setTimeout(() => this.someMethod(), 1000);