js 类数组

  • 什么是类数组,一开始我是懵逼的,直到我要学习ts才知道类数组这个东西

类数组:是一种相似数组的对象,并提供了一种用于访问原始二进制数据的机制,但不是真正的数组。js 中类数组对象有不少,例如argumentsNodeListHTMLCollectionjQuery


类数组拥有的特性

  1. length

    const a = document.getElementsByTagName("div")
    a.length
    
  2. 能够使用数字下标方式访问对象

    a[0]; // <div id="app"></div>
    
  3. 不能使用数组原型方法,如slice、pop等

    a.push(0) // 报错
    
  4. 使用 instanceof 操做不属于 Array

    a instanceof Array; // false
    a instanceof Object; // true
    
  5. 能够被转换为真数组

    const arr = Array.from(a)
    arr instanceof Array; // true
    
  6. 可自定义其余属性

    a.name = '666'
    

arguments 类对象

function fn() {
  console.log(arguments) // [Arguments] { '0': 1, '1': 2, '2': 3, '3': 4 }
}
fn(1,2,3,4)
posted @ 2022-07-05 17:45  王小美丶  阅读(270)  评论(0编辑  收藏  举报