簡體   English   中英

為什么Array.prototype是一個數組?

[英]Why Array.prototype is an Array?

我認為每個原型都應該是一個對象。

為什么?

Array.isArray(Array.prototype)// true

developer.mozilla.org什么都沒解釋

您假設每個原型都是Object是不正確的。

console.log(String.prototype)
console.log(Number.prototype)
console.log(Boolean.prototype)
console.log(Array.prototype)
console.log(Object.prototype)

輸出:

String {}
Number {}
Boolean {}
[]
Object {}

來自ECMAScript語言規范 - 15.4.4數組原型對象的屬性 (強調我的)

Array原型對象的[[Prototype]]內部屬性的值是標准的內置Object原型對象(15.2.4)。

Array原型對象本身就是一個數組 ; 它的[[Class]]是“Array”,它有一個length屬性(初始值為+0)和15.4.5.1中描述的特殊[[DefineOwnProperty]]內部方法。

嘗試在javascript控制台中輸入: typeof Array.prototype;

Array.prototype實際上是一個Array。 本頁詳細介紹了這一點。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype

它可能與事實有關, []Array簡寫。

所以Array.prototype指向[] Array.prototype.constructor指向function Array() { [native code] }

[].constructor也指向function Array() { [native code] }

所以在猜測時,它是以這種方式完成的,這樣你就可以互相使用Array[]

我不確定這是原因,但這是我最好的猜測。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM