![](/img/trans.png)
[英]If [] is [] and Array.prototype is [] why doesn't ([] == Array.prototype)
[英]Why Array.prototype is an Array?
您假設每個原型都是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.