首页 > 编程语言 > 详细

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

时间:2019-08-08 01:15:59      阅读:151      评论:0      收藏:0      [点我收藏+]

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

 

类数组:

//属性要为索引(数字)属性,必须要有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,

}

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

原文:https://www.cnblogs.com/rabbit-lin0903/p/11318553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!