JS === 类数组(伪数组)

// 今天跟成哥学习了类数组的相关用法,涨见识了,记录一下~

类数组:

//属性要为索引(数字)属性,必须要有length属性,最好要加上push

Array.prototype.push = function(target){
  
   obj[obj.length] = target;
   obj.length++;          
}
var obj = {
   
"2" : " a",
"3" :  "b",
"length" : 2,
"push" : Array.prototype.push
//
"splice":Array.prototype.splice

//当加上这个splice属性后,此对象会以数组的形式显现,但仍然是对象。
} 

obj.push(
'c'); ====> obj[obj.length] = target; 所以 obj[2] = "c" obj.length = 3
obj.push(
'd');  ====> obj[obj.length] = target; 所以 obj[3] = "d" obj.length = 4




======> 最后的
obj {
"2" : "c",
"3" : "d",
"length" : 4,

}

猜你喜欢

转载自www.cnblogs.com/rabbit-lin0903/p/11318553.html