1. 首先需要明白函数中的arguments
arguments是什么呢? argument里面存储了所有传递过来的参数,是一个类数组,
什么是类数组呢? 当一个数组的隐式原型(__proto__)Object的时候就可以证明这是一个类数组
类数组和普通数组的区别? 类数组中没有普通数组中常用的方法,例如unshift、pop、push等,因为普通数组的隐式原型(__proto__)是执行Array,使用上面的方法时会从原型上面查找
2. unshift会更改元素组 返回新数组的长度
3. 在Array的原型上手写 unshift 方法
Array.prototype.MultipleUnshift = function () {
// 遍历原数组的长度 加上 传递过来参数的长度
for (let i = this.length + arguments.length; i > 0; i--) {
// 把原数组中的元素放入新数组的最后
this[i - 1] = this[i - arguments.length - 1]
}
// 遍历传递进来的数组
for (let i = 0; i < arguments.length; i++) {
// 从前开始给新数组赋值
this[i] = arguments[i]
}
// 返回新数组
return this.length
}
let arr1 = [4, 5, 6, 7, 8, 9]
console.log(arr1.MultipleUnshift(1, 2, 3, 4, 0))
console.log(arr1)