Skip to content Skip to sidebar Skip to footer

"parasitic Combination Inheritance" In Professional Javascript For Web Developers

Professional JavaScript for Web Developers, Third Edition by Nicholas C. Zakas (Wrox, 2012, p.210-215 describes 'Parasitic Combination Inheritance' using the following function: fu

Solution 1:

The assignment to "constructor" is not mandatory as the assignment to "prototype" is. The reason to do it is that function prototypes usually come with the "constructor" property set by default. It might be useful for libraries that copy objects since you can get a reference to that object's constructor from the object itself.

functionFoo(){
}

obj = newFoo();

console.log(obj.constructor); //function Foo

Solution 2:

Demo You overwrite constructor prototype so you lose SubType.prototype.constructor and if you want later to know object constructor you ought to explicitly set it...

functionobject(o){
    functionF(){}
    F.prototype = o;
    returnnewF();
}

functioninheritPrototype(subType, superType) {
    var prototype = object(superType.prototype); 
    prototype.constructor = subType; //if omit (new SubType()).constructor === superType 
    subType.prototype = prototype; 
}

functionSuperType(name){
    this.name = name;
}

functionSubType(name, age){
    SuperType.call(this, name);
    this.age = age;
}

inheritPrototype(subType, superType);

Post a Comment for ""parasitic Combination Inheritance" In Professional Javascript For Web Developers"