6

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

function inheritPrototype(subType, superType) {
    var prototype = object(superType.prototype); 
    prototype.constructor = subType; 
    subType.prototype = prototype; 
}

I have yet to figure out what the assignment of subType to the prototype.constructor does or is supposed to do. Unless I am missing something, the output I get using the example code is the same:

Without "augment object" (prototype.constructor = subType;) in inheritPrototype: http://jsfiddle.net/Q22DN/

With "augment object" (prototype.constructor = subType;) in inheritPrototype http://jsfiddle.net/eAYN8/

Can this really be a line of purposeless code? Thank you for your explanation!

Kevin Swallow
  • 145
  • 1
  • 1
  • 5

2 Answers2

3

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.

function Foo(){
}

obj = new Foo();

console.log(obj.constructor); //function Foo
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • is there any reason to prefer this pattern to goog.inherits? http://docs.closure-library.googlecode.com/git/closure_goog_base.js.source.html#line1466 I like how it sets the `superClass_` in child so you can override parent methods and still call them from a child. – HMR Jun 05 '13 at 00:40
  • @HMR: Different inheritance patterns tend to work better with different features (how you interact with "regular" classes, if you can handle multiple inheritance, private or protected variables, super, etc). Personally, I tend to program in a more "functional" style so I don't actually use that many classes and when I do use them the hierarchy is usually very flat so superclass and inheritance stuff doesn't end up mattering much. – hugomg Jun 05 '13 at 03:19
  • Both missingno and Givi are correct in that the assignment to constructor is not mandatory and without it the subType (or child) instance constructor === SuperType, and resetting it to SubType will make that information available later. Perhaps that is all there is to it. Thanks! – Kevin Swallow Jun 06 '13 at 00:52
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...

function object(o){
    function F(){}
    F.prototype = o;
    return new F();
}

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

function SuperType(name){
    this.name = name;
}

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

inheritPrototype(subType, superType);
Givi
  • 1,674
  • 2
  • 20
  • 35
  • Where does the object created with `new F();` which aliases an empty instance of `SuperType` go? It will be still allocated in the heap, right? Therefore when I access the name property `var sub = new SubType("Name", 16); console.log(sub.name)` a prototype lookup is made and the prototype chain is climbed and the instance of F is in the chain too? If I type `sub.__proto__` however I get `SubType {}` and not `F {}` as the prototype... Where did F go? – tonix May 26 '15 at 09:51