對象調用 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