记住一句话: 伪数组是一个Object,数组是Array。
JavaScript的内置函数继承与 Object.prototype
。
可以认为new Array()
和[]
创建出来的数组对象, 都拥有Object.prototype
属性值。
var obj = {}; //拥有Object.prototype的属性值
var arr = []; //由于Array.prototype的属性继承自Object.prototype, 那么它就是拥有两个属性
// 即Array.prototype和Object.prototype
注意: 对象没有数组的Array.prototype属性值
数组的基本特征: 索引(下标)取值
var obj = {};
var array = [];
obj[0] = "L";
array[0] = "L";
console.log(obj); // {0: "L"}
console.log(obj[0]); // L
console.log(array[0]); // L
console.log(obj.length); // undefined
console.log(array.length); // 1
伪数组类似于Python中的字典
var fakeArray = {
"0":"胡珺",
"1":23,
length:2
};
for (var i=0;i<fakeArray.length;i++){
console.log(fakeArray[i])
}
arguments
document.getElementsByTags
)$("div")
)注意: 伪数组是一个对象
简单的一个应用
var obj = {
0: ‘a‘,
1: ‘b‘,
2: ‘c‘,
length: 3
}
;[].push.call(obj, ‘d‘);
console.log([].slice.call(obj))
;[].forEach.call(obj, function (num, index) {
console.log(num)
})
原文:https://www.cnblogs.com/liudemeng/p/11510275.html