[英]Javascript: Accessing superclass objects from nested class
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的值?
發現執行此操作的最佳方法是在嵌套類的構造函數中將“ this”作為參數傳遞,如下所示:
a = new function() {
this.x=2;
B=function(sup) {
this.y=sup.x;
}
this.b=new B(this);
}
alert(a.b.y); // Displays 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.