类数组

var obj = {
    '2' : 3,  
    '3' : 4,
    'length' : 2,
    'splice' : Array.prototype.splice,
    'push' : Array.prototype.push
};
obj.push(1);
obj.push(2);
console.log(obj)  // [empty × 2, 1, 2, splice: ƒ, push: ƒ]

// 原理
// Array.prototype.push = function(elem){
//     this[this.length] = elem;  // 当前元素最后位置添加类容
//     this.length ++;            // 让length 加一
// }

// obj[2] = 1;
// obj[3] = 2;

 

posted @ 2019-02-09 00:15  阿|明  阅读(160)  评论(0编辑  收藏  举报