函数()和()=>在JavaScript中有什么区别
我想知道这是什么区别:
选项A:
var elements = ['a', 'b', 'c', 'd']; elements.forEach(function(e){ console.log('element is: ' + JSON.stringify(e, null, 2)); });
选项B:
var elements = ['a', 'b', 'c', 'd']; elements.forEach((e) => { console.log('element is: ' + JSON.stringify(e, null, 2)); });
选项B比A更好/更快? 或者是一样的?
谢谢
选项B对JavaScript ES6(也称为ES2015)使用更高版本的语法。 它只是逐渐被浏览器和节点所支持; 通常如果你想使用它,你会使用像Babel这样的东西来进行传输 (这将有效地将选项B的代码转换成选项A)。
https://babeljs.io/docs/learn-es2015/
https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/