let obj = {
2: ‘a‘,
3: ‘b‘,
length: 2,
push: Array.prototype.push
}
obj.push(‘c‘, ‘d‘);
console.log(obj)
obj = {2:‘c‘, 3: ‘d‘, length: 4, push: Array.prototype.push}
Array.prototype.myPush = function(...args) {
for(let i = 0; i < args.length; i++) {
this[this.length++] = args[i];
}
return this.length;
}
let obj1 = {
2: ‘a‘,
3: ‘b‘,
length: 2,
push: Array.prototype.myPush
}
obj1.push(‘c‘, ‘d‘);
console.log(obj1) //{2:‘c‘, 3: ‘d‘, length: 4, push: Array.prototype.myPush}
原文:https://www.cnblogs.com/wanqiblog/p/13488202.html