類陣列

howhy發表於2024-05-10

類陣列:屬性為索引(數字字串)屬性,必須有length屬性,最好加上push

   var obj = {
      '1': 'aa',
      '2': 22,
      'length': 2,
      'push': Array.prototype.push
    }
    // Array.prototype.push = function (item) {
    //   obj[obj.length] = item
    //   obj.length++
    // }
    obj.push('bbb')
    console.log(obj)//{1: 'aa',  2: 'bbb', length: 3, push: ƒ}

相關文章