鍍金池/ 問答/Python  HTML/ 為什么 Array.prototype 的類型是數(shù)組, 而 Object.pr

為什么 Array.prototype 的類型是數(shù)組, 而 Object.prototype 是對象

看看下面:

Array.isArray(Array.prototype) //true.

當從chrome瀏覽器控制臺打印出來后

[constructor: ?, concat: ?, pop: ?, push: ?, shift: ?,?…]

但是卻不能得到 Array.prototype[0], Array.prototype[1]... or Array.prototype.length

但是Object.prototype 卻是

{constructor: ?, __defineGetter__: ?, __defineSetter__: ?,
hasOwnProperty: ?, __lookupGetter__: ?,?…}

為什么???

回答
編輯回答
礙你眼
  1. Array.prototype類型為數(shù)組可能是Array.prototype上掛的方法都是Array類型適用的,而不能通過Object調(diào)用,為了保證這個一致性吧。
  2. 數(shù)組也是能添加非數(shù)字索引的屬性的,至于lenght,你肯定能取到為0。
2018年7月16日 07:34
編輯回答
陌南塵

Array.prototype 打印得出的是它的構(gòu)造函數(shù) 和原型,就是它的實例 空對象
如Number.prototype 是Number { 0 }
String.prototype 是 String { "" }
所有對象都是用.的方式去獲取

2018年5月25日 15:51
編輯回答
枕邊人

Array.prototype是一個數(shù)組,同時也是一個對象,意味著除了0,1,2。。。等數(shù)字索引外還可有有其他的鍵,它現(xiàn)在是一個空數(shù)組,當然沒有0,1,2項了

2018年3月26日 01:07
編輯回答
掛念你

請參考 Array.prototype - JavaScript | MDN 中這段話:

Array.prototype.constructor

所有的數(shù)組實例都繼承了這個屬性,它的值就是 Array,表明了所有的數(shù)組都是由 Array 構(gòu)造出來的。

Array.prototype.length

上面說了,因為 Array.prototype 也是個數(shù)組,所以它也有 length 屬性,這個值為 0,因為它是個空數(shù)組。
2018年1月7日 11:39