有没有在JavaScript中将私有成员从基类继承到子类的方法?如何在JavaScript中继承私有成员?
我要实现的是这样的:
function BaseClass() {
var privateProperty = "private";
this.publicProperty = "public";
}
SubClass.prototype = new BaseClass();
SubClass.prototype.constructor = SubClass;
function SubClass() {
alert(this.publicProperty); // This works perfectly well
alert(this.privateProperty); // This doesn't work, because the property is not inherited
}
我如何能实现一类般的仿真,就像在其他OOP语言(如C++)。在那里我可以继承私有(保护)的属性?
谢谢 大卫·施雷伯
此模式不添加私人财产。它只在BaseClass函数中添加一个名为privateProperty的局部变量。 – erikkallen 2009-11-28 17:36:00
请参阅http://stackoverflow.com/questions/1437712/how-to-override-private-variable-in-javascript/1438592#1438592对于我对这种语言混蛋的看法;学习JS的语义,而不是模仿C++ – Christoph 2009-11-28 17:43:28