1
a = new function() {
this.x=2;
B=function() {
this.y=super.x;
}
this.b=new B();
}
alert(a.b.y); // Expecting 2
在上面,在super
中存在解析错误。定义B类时如何访问x的值?Javascript:从嵌套类访问超类对象
a = new function() {
this.x=2;
B=function() {
this.y=super.x;
}
this.b=new B();
}
alert(a.b.y); // Expecting 2
在上面,在super
中存在解析错误。定义B类时如何访问x的值?Javascript:从嵌套类访问超类对象
实测值来做到这一点的最好的方法是通过“这个”如在嵌套类的构造函数的参数,这样的 -
a = new function() {
this.x=2;
B=function(sup) {
this.y=sup.x;
}
this.b=new B(this);
}
alert(a.b.y); // Displays 2
这工作,但我不知道你的代码是在任何情况下正确
a = new function() {
var x=2;
B=function() {
this.y=x;
}
this.b=new B();
}
alert(a.b.y); //alerts 2
alert(a.x) //alert undefined becuase x is private
存在的JavaScript没有super
,如果你读here你可以看到你可以通过实现在JavaScript inehritance超级方法