Tôi đã quan tâm để tìm ra cách để trả lời của bạn câu hỏi, và đây là những gì tôi có thể làm.
Bạn sẽ cần helper này:
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;
})();
helper này sẽ cho phép bạn xử lý nhiều tài sản thừa kế. Bí quyết là sao chép biến được bảo vệ từ đối tượng cơ sở sang đối tượng mới của bạn (con).
Để chứng minh bạn nó làm việc, đây là một ví dụ:
// 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);
Và đây là các bài kiểm tra:
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"
Không ............ –
Lời khuyên của tôi, như thường lệ, là không ép buộc khả năng hiển thị vào JavaScript. * Một số * điều có thể với đóng cửa vv, nhưng ngôn ngữ không được thiết kế cho điều đó. Nó sẽ làm cho mã của bạn phức tạp hơn. Thay vào đó, hãy ghi lại các phương thức của bạn một cách chính xác như riêng tư hoặc công khai và nếu các nhà phát triển khác không tuân theo đặc tả của bạn, đó là vấn đề của họ. –
Btw, các đối tượng nguyên mẫu có thể được tăng cường (chúng không bị đóng kín) - không có gì ngăn cản kẻ xâm nhập thêm các phương thức mới vào đối tượng nguyên mẫu. Vì vậy, có một tài sản có thể truy cập chỉ thông qua các phương pháp nguyên mẫu sẽ không được an toàn anyway (ngay cả khi nó đã có thể). –