Skip to content

JavaScript 类数组遍历

类数组是类似数组的对象,在对象上定义一些数组基础属性,比如 length 索引值

shell
# 常见类数组
arguments、DOM 集合(NodeList/HTMLCollection)、{ length: 3, 0: 'a', 1: 'b' }

# 直接遍历
for 循环(最通用、兼容最好)
for…of(ES6,支持大部分类数组)
Array.prototype.forEach.call()

# 转数组后遍历(最常用、最舒服)
Array.prototype.slice.call() + 遍历
Array.from () + 遍历
展开运算符 + 遍历
Array.prototype.slice.call() + 遍历
Array.prototype.slice.call() + 遍历