不要使用 iterators。使用高階函數(shù)例如map()和reduce()替代for-of揩徊。
注:為什么?這加強(qiáng)了我們不變的規(guī)則嵌赠。處理純函數(shù)的回調(diào)值更易讀塑荒,這比它帶來的副作用更重要。
let arr = [1, 2, 3, 4, 5, 6];
let power = (value, time) => {
return Math.pow(value, time)
};
// map方法
let mapResult = arr.map((value) => {
return power(value, 2)
});
console.log(mapResult);
// reduce方法
let reduceResult = arr.reduce((x, y) => {
return x + y
});
console.log(reduceResult);