什么是伪数组?
1. 伪数组的基本特征
① 无法直接调用数组方法
② 可以用对真正数组遍历方法来遍历它们
③ 具有 length 属性
2. 常见的伪数组
- 函数内部的
arguments
参数 - DOM 对象列表(调用
document.getElementsByTagName
,document.childNodes
之类的,它们都返回的 NodeList 对象) - jQuery 对象(
$("div")
)
注意:伪数组是一个对象
如何将伪数组转化为标准数组?
- 声明一个空数组,遍历伪数组把它们添加到所声明的空数组中 (最原始的办法,不推荐)
var Lis = document.querySelectorAll('li');
var arr = [];
for (var i = 0; i < Lis.length; i++) {
arr[arr.length] = Lis[i]
- 使用
Array.prototype.slice.call()
或Array.prototype.slice.apply()