मैं जवाब देने के लिए एक रास्ता खोजने के लिए दिलचस्पी थी अपने सवाल, और यहां मैं क्या करने में सक्षम था।
var ProtectedHandler = (function() {
/// <Sumarry>
/// Tool to handle the protected members of each inheritance.
/// </Summary>
/// <param name="current">Current protected variable.</param>
/// <param name="args">The arguments variable of the object.</param>
/// <param name="callback">The function to initialise the variable in the 'object'.</param>
/// <param name="isParent">Is this the ultimate base object.</param>
function ProtectedHandler(current, args, callback, isParent) {
this.child = getChild(args);
if (callback)
this.callback = callback;
if (isParent)
this.overrideChild(current);
}
// Get the ProtectedHandler from the arguments
var getChild = function (args) {
var child = null;
if (args.length > 0 && (child = args[args.length - 1]) && child.constructor === ProtectedHandler)
return child;
};
// Chain Initialise the protected variable of the object and its inheritances.
ProtectedHandler.prototype.overrideChild = function (newValue) {
if (this.callback != null) {
this.callback(newValue);
}
if (this.child != null) {
this.child.overrideChild(newValue);
}
};
// Static function to create a new instance of the protectedHandler object.
ProtectedHandler.handle = function (protected, arguments, callback, isParent) {
return new ProtectedHandler(protected, arguments, callback, isParent);
};
return ProtectedHandler;
})();
यह सहायक आप एकाधिक विरासत को संभालने के लिए अनुमति देगा:
आप इस सहायक की आवश्यकता होगी। यह चाल संरक्षित चर को मूल वस्तु से आपकी नई वस्तु (बच्चे) में कॉपी करना है।
आप यह काम कर रहा साबित करने के लिए, यहाँ एक उदाहरण है:
// That's the default extends function from typescript (ref: http://www.typescriptlang.org/)
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var BaseClass = (function() {
function BaseClass() {
// Members
var private = {},
protected = {},
public = this;
// Constructor
ProtectedHandler.handle(protected, arguments, function() {
protected.type = "BaseClass";
}, true);
// Methods
protected.saySomething = function() {
return "Hello World";
};
public.getType = function() {
return protected.type;
};
}
return BaseClass;
})();
var Person = (function (_super) {
__extends(Person, _super);
function Person(name) {
// Members
var private = {},
protected = {},
public;
// Constructor
_super.call(public = this, ProtectedHandler.handle(protected, arguments, function (p) {
protected = p; //This is required to copy the object from its base object.
protected.name = name;
protected.type = "Person";
}));
//Method
public.getName = function() {
return protected.name;
};
public.saySomething = function() {
return protected.saySomething();
};
}
return Person;
})(BaseClass);
var Child = (function (_super) {
__extends(Child, _super);
function Child(name) {
// Members
var private = {},
protected = {},
public;
// Constructor
_super.call(public = this, name, ProtectedHandler.handle(protected, arguments, function (p) {
protected = p; //This is required to copy the object from its base object.
protected.type = "Child";
}));
//Method
public.setName = function (value) {
return protected.name = value;
};
}
return Child;
})(Person);
और यहाँ है परीक्षण:
var testBase = new BaseClass();
testBase.getType(); //"BaseClass"
testBase.saySomething; //undefined
var testPerson = new Person("Nic");
testPerson.getType(); //"Person"
testPerson.saySomething(); //"Hello World"
testPerson.name; //undefined
testPerson.getName() //"Nic"
testPerson.setName; //undefined
var testChild = new Child("Bob");
testChild.getType(); //"Child"
testChild.saySomething(); //"Hello World"
testChild.name; //undefined
testChild.getName(); //"Bob"
testChild.setName("George");
testChild.getName(); //"George"
नहीं ............ –
मेरी सलाह, अक्सर, जावास्क्रिप्ट में दृश्यता को मजबूर नहीं करना है। * बंद * आदि के साथ कुछ * चीजें संभव हैं, लेकिन भाषा इसके लिए डिज़ाइन नहीं की गई है। यह आपके कोड को और अधिक जटिल बना देगा। इसके बजाए, अपनी विधियों को निजी या सार्वजनिक रूप से सही तरीके से दस्तावेज करें और यदि अन्य डेवलपर आपके विनिर्देश का पालन नहीं करते हैं, तो यह उनकी समस्या है। –
बीटीडब्ल्यू, प्रोटोटाइप ऑब्जेक्ट्स को बढ़ाया जा सकता है (वे सील नहीं किए जाते हैं) - प्रोटोटाइप ऑब्जेक्ट में नए तरीकों को जोड़ने के लिए घुसपैठियों को कुछ भी नहीं रोकता है। इसलिए, प्रोटोटाइप विधियों के माध्यम से केवल एक संपत्ति सुलभ होने के बावजूद सुरक्षित नहीं होगा (भले ही यह संभव हो)। –