对象调用 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);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM