【发布时间】:2012-02-28 04:57:36
【问题描述】:
我正在尝试调用必须从当前对象访问私有属性的继承方法。但它只访问公共的,有什么问题?
我的测试代码应该提醒两个变量:
function ParentClass(){
//Priviliged method to show just attributes
this.priviligedMethod = function(){
for( var attr in this ){
if( typeof(this[ attr ]) !== 'function' ){
alert("Attribute: " + this[ attr ]);
}
}
};
}
function ChildClass(){
// Call the parent constructor
ParentClass.call(this);
var privateVar = "PRIVATE VAR";
this.publicVAR = "PUBLIC VAR";
}
// inherit from parent class
ChildClass.prototype = new ParentClass();
// correct the constructor pointer because it points to parent class
ChildClass.prototype.constructor = ChildClass;
var objChild = new ChildClass();
objChild.priviligedMethod();
jsfiddle 版本:http://jsfiddle.net/gws5s/6/
提前致谢, 亚瑟
【问题讨论】:
标签: javascript oop inheritance private private-members