JavaScript 类数组

类数组属性要为(数字索引),必须有length属性,最好加上push
//类数组
        var obj = {
            "0": 'a',
            "1": 'b',
            "2": 'c',
            'length': '3',
            'push': Array.prototype.push,
            'splice': Array.prototype.splice
        }

类数组的实现

//类数组的实现
        Array.prototype.push = function (target) {
            obj[obj.length] = target;
            obj.length++;
        }

 var obj = {
            '2':'a',
            '3':'b',
            'length':2,
            'push':Array.prototype.push
        }
        obj.push('c');
        obj.push('d');//2:c,3:d

 

 

posted @ 2019-03-31 17:13  110来了  阅读(168)  评论(0编辑  收藏  举报