2012-02-12 20 views
9

Revealing Prototype kalıbını kullanan sınıfları nasıl alabilirim/uzatırım? private değişkenlerini ve protected işlevlerini gerçekleştirmenin bir yolu var mı?JS'de mirasın nasıl uygulanacağı Prototip modelinin ortaya çıkarılması?

Örnek temel nesne:

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 
    }; 
}(); 

cevap

7

JavaScript hiçbir koruma değişkenler/özellikleri vardır. Yine de, özel değişkenler yalnızca prototipinizin "gizli yardımcı programları" olduğunda, olası kapsamı sınıfları aynı kapsamda bildirdiğinizde "özel" değişkenleri yeniden kullanabilirsiniz.

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)); 
})(); 

Bu Amerikalının prototip Kişinin prototipten automagicallylar klon(), toJSON() ve fromJSON() işlevlerini devralır anlamına miras JavaScript yoludur. Tabii ki, Bunu gerekir ve, yardımcı programın işlevlerini yeniden verebilecek daha modül benzeri şekilde kullanmak istemiyorsanız Ve özellik Tabii

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

olduğunu yani sadece onları kopyalamak:

(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

Veraset ve prototip hakkında daha fazla bilgi edinmek istiyorsanız, http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index.html adresini ziyaret edebilirsiniz. 3. nokta mirasın başladığı yer. –

+0

Ne yazık ki bu link bir 404 hatası veriyor. –

+1

@Programmer_D: http://robotlolita.me/2011/10/09/understanding-javascript-oop.html adresine taşındı. Ve tabii ki de [eski sürümü görebilir] (http://web.archive.org/web/20130127102509/http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index .html) – Bergi

İlgili konular