js类数组的理解

本质上来说对象是满足了一定条件的数组,类数组的使用目的在于使得
一个对象既有数组的特性也具有对象的特性。

var arr = [2,3];//(1)
var obj1 = {
    '2':2,
    '3':3,
    'length':2,
    'push':Array.prototype.push,
    'splice':Array.prototype.splice

};//(2)

(1) [2, 3]//arr返回
0: 2
1: 3
length: 2
proto: Array(0)
(2)obj1返回
Object(3) [empty2,2:2, 3: 3, push: ƒ, splice: ƒ]
2: 2
3: 3
length: 2
push: ƒ push()
splice: ƒ splice()
proto: Object
对象添加length,push,splice属性后形成类数组,大括号变为方括号,key值变为下标,注意此时1号位为empty
2,length: 2。

var obj1 = {//(3)
    '2':2,
    '3':3,
    'length':3,
    'push':Array.prototype.push,
    'splice':Array.prototype.splice

};
 obj1.push(4);//往类数组push一个数4;

(3)obj1返回
Object [empty × 2, 4, 3: 3, push: ƒ, splice: ƒ]
2: 4
3: 3
length: 3
push: ƒ push()
splice: ƒ splice()
proto: Object//这时length++变为3,注意原来2被4挤掉了!!!。以此类推再push(5)进去length++变为4,注意原来3被5挤掉了!!!

var obj1 = {//(4)
    '2':2,
    '3':3,
    'length':2,
    'push':Array.prototype.push,
    'splice':Array.prototype.splice

};
 obj1.push(4);
 obj1.push(5);
 obj1.push(6);

(4)obj1返回
Object(4) [empty × 2, 4, 5, 6, push: ƒ, splice: ƒ]
2: 4
3: 5
4: 6
length: 5
push: ƒ push()
splice: ƒ splice()
proto: Object
当length大于4时,再push一个数才能往后边添加。

你可能感兴趣的:(javascript,js,JavaScript)