2012-02-12 72 views
9

如何繼承/擴展使用Revealing Prototype模式的類? 有沒有辦法使private變量和函數protected如何在JS中實現繼承展示原型模式?

實施例基礎對象:

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

回答

7

有在JavaScript中沒有保護的變量/屬性。雖然,當你在同一個作用域中聲明繼承類時,你可以重複使用「私有」變量,當私有變量只是你的原型的「隱藏實用程序」時,在你的情況下這似乎是可能的。

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

這是繼承的JavaScript的方式,這意味着美國的原型來自人的原型自動將繼承的clone()的toJSON()和fromJSON()函數。當然可以覆蓋。並且該功能

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

當然,如果你不需要那麼,想使用更多的模塊狀的方式,你可以重複使用的實用功能,即只需將它們複製:

(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

如果你想了解關於繼承和原型的更多信息,請點擊這裏:http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index.html我認爲你可以去點3繼承開始。 – 2012-02-23 06:21:12

+0

不幸的是,該鏈接現在提供了404錯誤。 – 2015-02-12 11:57:19

+1

@Programmer_D:它已移至http://robotlolita.me/2011/10/09/understanding-javascript-oop.html。當然你也可以[查看舊版本](http://web.archive.org/web/20130127102509/http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index .html) – Bergi 2015-02-12 12:05:53