掘金上看到一道有趣的面试题

原创 人马在冬至追风 随笔 随笔 295阅读 2018-12-28 09:28:20 举报

const a = [1, 2, 3, 4, 5]
a.multiply()
console.log(a) //[1,2,3,4,5,1,4,9,16,25]

如何让以上代码输出此结果?

评论 ( 2 )
最新评论
JadyCM 2019-01-03 10:46:11 2F

const a = [1,2,3,4,5]
Array.prototype.mutiply = function(){
this.push(...this.map(num => num*num))
}
a.mutiply()
console.log(a)

Hypocrisy丶Cc 2018-12-28 15:03:20 1F