类数组

什么是类数组

  • 它首先是一个对象,其次与数组相似,它拥有 length 属性,但却不能使用数组的方法(Array.prototype)。只要一个对象Object,拥有 length 属性,那它就是一个类数组对象。

类数组的特性

  • 属性要为索引(数字属性),必须有length属性,最好加上push
  Array.prototype.push = function (target) {
    this[this.length] = target;
    this.length ++;
  }
  let obj = {
    "0": 'a',
    "1": 'b',
    "2": 'c',
    length: 3,
    push: Array.prototype.push,
    splice: Array.prototype.splice
  }
posted @ 2022-03-11 00:35  HuangBingQuan  阅读(213)  评论(0编辑  收藏  举报