为什么创建一个JS单行函数以返回我们已经可以访问的值?

时间:2018-08-23 17:27:21

标签: javascript anonymous-function

以下代码是示例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之后的括号内定义}}。

0 个答案:

没有答案