面试题 实现
const a = [1, 2, 3, 4, 5];
// Implement this
a.multiply();
console.log(a); // [1, 2, 3, 4, 5, 1, 4, 9, 16, 25]
要求的是对a修改
const a = [1, 2, 3, 4, 5];
// Implement this
Array.prototype.multiply = function() {
this.forEach(x => this.push(x * x))
}
a.multiply();
console.log(a); // [1, 2, 3, 4, 5, 1, 4, 9, 16, 25]
这种就不是对a修改了,而是返回一个值
const a = [1, 2, 3, 4, 5];
// Implement this
Array.prototype.multiply = function() {
b=this.map((x)=>{
return x*x
})
c=[...this, ...b]
return c
}
console.log(a.multiply()); // [1, 2, 3, 4, 5, 1, 4, 9, 16, 25]