2010-04-11 6 views
6

mi sono imbattuto in questa slide: http://www.slideshare.net/stoyan/javascript-patterns#postCommentreset costruttore javascript: che cos'è?

a pagina 35:

Opzione 5 + Super + costruttore ripristinate

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

io non capisco. Qualcuno può spiegare per quale motivo è l'ultima riga?

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

Grazie

risposta

11

questo dà una spiegazione http://phrogz.net/JS/Classes/OOPinJS2.html

In particolare

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

Grazie. Javascript è una lingua davvero strana. :) – Sake

+2

Lo è. Personalmente non userei il nome 'costruttore 'per memorizzare il costruttore perché' costruttore' ha già un significato in JavaScript. (Solo non utile.) – bobince