tags:

views:

72

answers:

1

I came across this slide: http://www.slideshare.net/stoyan/javascript-patterns#postComment

at page 35:

Option 5 + super + constructor reset

function inherit(C, P) {
    var F = function(){};
    F.prototype = P.prototype;
    C.prototype = new F();
    C.uber = P.prototype;
    C.prototype.constructor = C;  // WHY ???
}

I don't get it. Can anybody please explain what the last line for ?

    C.prototype.constructor = C;  // WHY ???

Thanks

+7  A: 

This gives an explanation http://phrogz.net/JS/Classes/OOPinJS2.html

In particular

Cat.prototype = new Mammal();        // Here's where the inheritance occurs 
Cat.prototype.constructor=Cat;       // Otherwise instances of Cat would have a constructor of Mammal 
Jonathan
Thanks. Javascript is a really weird language. :)
Sake
It is. I personally wouldn't use the name `constructor` for storing the constructor because `constructor` already has a meaning in JavaScript. (Just not a useful one.)
bobince