[英]JavaScript Prototype properties initialized in constructor does not overrides with new prototype assigned
例子:
function ChildClass() {
**ChildClass.prototype.Field1 = "Field1 value";**
}
ChildClass.prototype = {};
var childInstance = new ChildClass();
print(childInstance.Field1);
為什么我們仍然可以訪問 childInstance.Field1?
您的構造函數正在添加該屬性。 當您調用構造函數以創建新實例時,該字段將添加到原型中。
因為您是從構造函數內部在原型上設置該屬性的,該構造函數在ChildClass.prototype = {};
之后運行ChildClass.prototype = {};
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.