首頁  >  文章  >  web前端  >  區分 proto 和 Constructor.prototype:關鍵區別是什麼?

區分 proto 和 Constructor.prototype:關鍵區別是什麼?

DDD
DDD原創
2024-10-21 10:32:03856瀏覽

Differentiating proto and Constructor.prototype: What's the Key Distinction?

理解proto 和Constructor.prototype

prototype中密切相關的概念,常導致混淆。本文旨在闡明它們的差異。

__proto__:

proto 是 JavaScript 物件的內部屬性,指向其原型物件。原型物件包含由該物件的實例繼承的屬性和方法。物件從其建構子繼承其 proto 屬性。

在範例中,

newtoy.__proto__ 傳回 Gadget.prototype 對象,其中包含繼承的 rating 屬性。

constructor.prototype:

函數的

constructor.prototype 屬性引用原型函數的物件。當使用 new 關鍵字建立物件時,其建構子的原型將成為新物件的原型。

在範例中,

newtoy.constructor.prototype 傳回Gadget.prototype 對象,具有繼承的 rating 屬性。

原型鏈:

兩者

proto 和constructor.prototype 參與原型鏈,原型鍊是JavaScript 中的一種機制,使物件能夠從其原型物件繼承屬性和方法。

newtoy.__proto__.constructor.prototype.constructor。 prototype.constructor.prototype 返回Gadget.prototype 對象,該對象繼承自Function.prototype 並最終以Object.prototype.

Internet Explorer:

Internet Explorer 沒有

proto 屬性。要在此上下文中檢查 null ,可以使用 hasOwnProperty() 方法來確定物件是否包含特定屬性。

例如:

以上是區分 proto 和 Constructor.prototype:關鍵區別是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn