2010-04-11 7 views
6

je suis tombé sur cette diapositive: http://www.slideshare.net/stoyan/javascript-patterns#postCommentjavascript constructeur reset: Qu'est-ce que c'est?

à la page 35:

Option 5 + super + constructeur réinitialisés

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

Je ne comprends pas. Quelqu'un peut-il s'il vous plaît expliquer à quoi sert la dernière ligne?

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

Merci

Répondre

11

Cela donne une explication http://phrogz.net/JS/Classes/OOPinJS2.html

En particulier

Cat.prototype = new Mammal();  // Here's where the inheritance occurs 
Cat.prototype.constructor=Cat;  // Otherwise instances of Cat would have a constructor of Mammal 
+3

Merci. Javascript est une langue vraiment bizarre. :) – Sake

+2

C'est. Personnellement, je n'utiliserais pas le nom 'constructor' pour stocker le constructeur car' constructor' a déjà une signification dans JavaScript. (Juste pas utile.) – bobince

Questions connexes