当我更改为箭头功能时,它不起作用。发生了什么?我正在学习JS,非常感谢您的帮助
ES5
Array.prototype.m = function(cb) {
var arr = [];
for (var i = 0; i < this.length; i++) {
arr.push(cb(this[i]));
}
return arr;
};
let n = [1,2,3,4].m((e) => e * 2);
ES6
Array.prototype.m = (cb)=> {
let arr = [];
for (let i = 0; i < this.length; i++) {
arr.push(cb(this[i]));
}
return arr;
};
let n = [1,2,3,4].m((e) => e * 2);