简体   繁体   中英

JavaScript Prototype properties initialized in constructor does not overrides with new prototype assigned

Example:

function ChildClass() {
    **ChildClass.prototype.Field1 = "Field1 value";**
}

ChildClass.prototype = {};

var childInstance = new ChildClass();
print(childInstance.Field1);

Why we still have access to childInstance.Field1?

Your constructor is adding the property. When you call the constructor to make a new instance, the field is added to the prototype.

Because you set that property on the prototype from inside the constructor, which runs after ChildClass.prototype = {}; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM