箭头函数的不使用return 直接返回数据

在MDN web文档中查看Array.from使用方法看到在箭头函数中不使用return就能返回数据,对与一些简单的返回方法,可以简便写法。

 

Array.from() 方法从一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。

console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

// 箭头函数常规写法 console.log(Array.from([1, 2, 3], (x) => { return x + x})); // expected output: Array [2, 4, 6]
// 箭头函数简单操作
console.log(Array.from([1, 2, 3], (x) => x + x)); // expected output: Array [2, 4, 6]

 

 当箭头函数箭头后面是简单操作时,直接去掉“{ }”,这样可以不使用return 就能会返回值。

posted @ 2020-01-05 09:29  只争朝夕,不负韶华  阅读(3597)  评论(0编辑  收藏  举报