function a(count) { return [1,2,3].map(val => val + count);
}function b(count) { return [1,2,3].map(fn);
}function fn(val) { return val + count;
}console.log('a' + a(9));console.log('b' + b(9));Function a中,map的callback使用arrow function直接定義在function a中,那么map的callback是可以使用count變量的。如果我想復用map的callback函數,如function b,那么這個callback如何取得count的值呢? 如上面的代碼,會拋出ReferenceError: count is not defined 錯誤。
Array.prototype.map() 的callback如何使用調用map的函數的變量?
茅侃侃
2018-09-12 13:34:48