对象调用 push 方法

/*
  Array.prototype.push = function A(val) {
    this[this.length] = val;
  // =>this.length 在原来的基础上加1
    return this.length;
  }
*/

let obj = {
  2: 3,
  3: 4,
  length: 2,
  push: Array.prototype.push
}
obj.push(1);
// => A(1) => this:obj => obj[obj.length]=1 => obj[2]=1 => obj.length=3
obj.push(2);
// => A(2) => this:obj => obj[obj.length]=2 => obj[3]=2 => obj.length=4
console.log(obj);
posted @ 2020-04-02 22:24  YuTing-721  阅读(1376)  评论(0编辑  收藏  举报