利用JavaScript重构数组的push方法 实现底层逻辑
Array.prototype.myPush = function(...args) {
const length = this.length;
for (let i = 0; i < args.length; i++) {
this[length + i] = args[i];
}
return this.length;
};
var arr = [1, 2, 3, 4, 5]
var res = arr.myPush('hello', 6)
console.log(res,arr);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/20111ef4fc6ec2f0e0562fbb63166aca.png)