1: 对象的push 方法:
数组中内置push方法:
当前数组的最大索引, this.length-1, 新增一项是: this.length
Array.prototype.push = function @@@() {
this[this.length] = val;
// this.length 在原来基础上加 1
return this.length; // 返回数组的长度
}
let obj = {
2: 3, // 1
3: 4, // 2
length: 2,
push: Array.prototype.push
}
obj.push(1);
obj.push(2);
console.log(obj);
------------------------------------------------------------------
obj.push(1);
this: obj; ==> @@@(1) ==> obj.length ==2 ==>obj[obj.length] = 1; ==> obj[2] == 1;
==>obj.length = 3;
-----------------------------------------------------------------
obj.push(2);
this: obj; ==> @@@(2) ==> obj.length ==3 ==> obj[obj.length] = 2; ==> obj[3] == 2;
==>obj.length = 4;
-------------------------------------------------------------------
result:
obj = {
2: 1;
3: 2;
length: 4,
push: Array.prototype.push
}