The basic syntax of fat arrow functions is:
(arg1, arg2, ...) => { ... }
However:
You can omit the ()
around the argument list if there's exactly one argument:
arg => { ... }
You can omit the {}
around the function body if you only have a single expression in the body, in which case return
is also implied:
arg => arg.foo
// means:
(arg) => { return arg.foo; }
Since callbacks of the form function (arg) { return arg.prop; }
are extremely common in Javascript, these two special cases to the syntax make such common operations extremely concise and expressive. E.g.:
arr.filter(foo => foo.bar)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…