簡體   English   中英

Constructor.prototype不在原型鏈中?

[英]Constructor.prototype not in the prototype chain?

相關: 關於原型鏈,基元和對象的混淆

在Firebug控制台中:

a = 12
a.constructor.prototype.isPrototypeOf(a) // prints 'false'

我認為這應該是true

a = 12創建一個原始數字,它與Number對象不完全相同。 為了屬性訪問的目的,基元被隱式地轉換為對象。

a = 12; //a is a primitive
b = new Number(12); //b is an object
a.constructor.prototype.isPrototypeOf(a); //false because a is primitive
b.constructor.prototype.isPrototypeOf(b); //true because b is an object

根據ECMAScript規范

使用參數V調用isPrototypeOf方法時,將執行以下步驟:

  1. 如果V不是對象,則返回false

嚴格來說,原始數字不是對象。

a = new Number(12);
a.constructor.prototype.isPrototypeOf(a) // prints 'true'

我不夠聰明,告訴你為什么我只知道它是這樣的。 是的,這很奇怪。

現在,你可以說12是一個原始的, new Number(12)是一個對象”。 但你怎么解釋這個?

(12).toFixed(3); // "12.000"

顯然,某些地方JavaScript正在決定原始可能也是一個對象。

為什么存在這種區別? 你如何在兩種形式之間轉換? 這對性能有何影響? 所有與此問題相關的問題我都沒有答案。

暫無
暫無

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

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