以下代码是示例question on arrow functions的答案。
var a = [
"We're up all night 'til the sun",
"We're up all night to get some",
"We're up all night for good fun",
"We're up all night to get lucky"
];
// These two assignments are equivalent:
// Old-school:
var a2 = a.map(function(s){ return s.length });
// ECMAscript 6 using arrow functions
var a3 = a.map( s => s.length );
// both a2 and a3 will be equal to [31, 30, 31, 31]
我是javascript新手,所以我想知道以这种方式定义函数的意义何在?在我看来,仅传递s.length
值本身会容易得多。即使使用更复杂的操作,将这些操作的输出保存到变量中并将该变量传递到函数a.map
或仅在{{1之后的括号内定义}}。