上QQ阅读APP看书,第一时间看更新
Arrow functions
You need to iterate over the elements of an array; normally, you would write something like this:
var data = ['Ronaldo', 'Messi', 'Maradona'];
data.forEach(function (elem) {
console.log(elem)
});
With the arrow functions, you can refactor your code and write something as follows:
var data = ['Ronaldo', 'Messi', 'Maradona'];
data.forEach(elem => {
console.log(elem);
});
The arrow (=>) operator defines a function in one line, making our code readable and ordered. First, you need to declare the inputs; the arrow will send these params to the function body defined by the operator:
// We could transform this
let sum = function(num) {
return num + num;
};
// Into just this
let sum = (num) => num + num;