Codog

关注微信公众号:Codog代码狗

0%

常见的类数组结构

arguments

NodeList

如何转为数组结构

  • 数组的slice方法:
1
2
3
4
5
6
// arguments, NodeList
Array.prototype.slice.call(ArrayLike)

// 是要有length属性,都可以调用slice方法;无length则返回空数组
obj = { '0': 0, 1: 1, 2: 2, length: 4 }
Array.prototype.slice.call(obj) // [0, 1, 2, empty]
  • Array.from方法
1
2
Array.from(arguments)
Array.from(document.querySelectorAll('div'))
  • 扩展运算符
1
2
[...arguments]
[...document.querySelectorAll('div')]