2012-02-12 10 views
9

Come si ereditano/estendono le classi che utilizzano il modello di Prototipo rivelatore? E c'è un modo per rendere le variabili e le funzioni privateprotected? oggetto baseCome implementare l'ereditarietà nel modello prototipo di JS Revealing?

Esempio:

myNameSpace.Person = function() { 

    this.name= ""; 
    this.id = 0; 

}; 

myNameSpace.Person.prototype = function(){ 
    var foo = function(){ 
     //sample private function 
    }; 
    var loadFromJSON = function (p_jsonObject) { 
     ... 
    }; 
    var toJSON = function() { 
     ... 
    }; 
    var clone = function (p_other) { 
     ... 
    }; 

    return { 
     loadFromJSON : loadFromJSON, 
     toJSON: toJSON, 
     clone: clone 
    }; 
}(); 

risposta

7

ci sono protette variabili/proprietà in JavaScript. Tuttavia, puoi riutilizzare le variabili "private" quando dichiari le classi ereditanti nello stesso ambito, il che sembra possibile nel tuo caso quando le variabili private sono solo "utilità nascoste" del tuo prototipo.

MyNamespace.Person = function Person(params) { 
    // private variables and functions, individual for each Person instance 
    var anything, id; 
    function execute_something() {} 

    // public properties: 
    this.name = ""; 
    this.getId = function getId(){ 
     // called a "privileged function", because it has access to private variables 
    } 
} 
MyNamespace.American = function(params) { 
    MyNamespace.Person.call(this, params); // inherit name and getId() 
} 

(function() { // new scope for 
    // hidden utility functions and other private things 
    function foo() { } 
    function helpJSON() { } 
    function fromJSON() { } 
    var bar; 

    (function(personProto) { // new scope for prototype module (not explicitly needed) 
     // "private" /static/ variables (and functions, if you want them private) 
     var personCount = 0; 

     personProto.clone = function clone() { 
      return this.constructor(myself); // or something 
     }; 
     personProto.toJSON = function toJSON() { 
      // use of helpJSON() 
     }; 
     personProto.fromJSON = fromJSON; // direct use 
    })(MyNamespace.Person.prototype); 

    (function(amiProto) { 
     // just the same as above, if needed 
     amiProto.special = function() { 
      // use foo() and co 
     }; 
    })(MyNamespace.American.prototype = Object.create(MyNamespace.Person.prototype)); 
})(); 

Questa è la via JavaScript di eredità, il che significa che il prototipo di americano eredita le funzioni clone(), toJSON() e fromJSON() automagicamente dal prototipo della persona. Ovviamente sovrascrivibile. E la funzione è

new MyNamespace.American() instanceof MyNamespace.Person; // true 

Naturalmente, se non avete bisogno di questo, e si desidera utilizzare il modo più moduli simili, si potrebbe riutilizzare le funzioni di utilità, vale a dire basta copiare:

(function() { 
    // hidden utility functions and other private things 
    var bar; 
    var personCount; 
    function foo() { } 
    function helpJSON() { } 
    function fromJSON() { } 
    function clone() { 
     return this.constructor(myself); // or something 
    } 
    function toJSON() { } 

    (function(personProto) { // new scope, not really needed 
     // private variables are useless in here 
     personProto.clone = clone; 
     personProto.toJSON = toJSON; 
     personProto.fromJSON = fromJSON; 
    })(MyNamespace.Person.prototype); 

    (function(amiProto) { // new scope, not really needed 
     // copied from personProto 
     amiProto.clone = clone; 
     amiProto.toJSON = toJSON; 
     amiProto.fromJSON = fromJSON; 
     // and now the differences 
     amiProto.special = function() { 
      // use foo() and co 
     }; 
    })(MyNamespace.American.prototype); 
})(); 
+1

Se vuoi saperne di più sull'ereditarietà e sul prototipo, leggi qui: http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index.html Penso che tu possa andare a punto 3 dove inizia l'ereditarietà. –

+0

Sfortunatamente quel link dà ora un errore 404. –

+1

@Programmer_D: viene spostato su http://robotlolita.me/2011/10/09/understanding-javascript-oop.html. E ovviamente puoi anche [visualizzare la vecchia versione] (http://web.archive.org/web/20130127102509/http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index .html) – Bergi

Problemi correlati