1
a = new function() {
    this.x=2;
    B=function() {
        this.y=super.x;
    }
    this.b=new B();
}

alert(a.b.y); // Expecting 2

In the above, there is a parse error in super. How can I access the value of x when defining the class B?

2 Answers 2

1

This works but i'm not sure that your code is correct

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

in any case there is no super in javascript, if you read here you can see how you could implement inehritance in javascript through a uber method

Sign up to request clarification or add additional context in comments.

Comments

0

Found the best way to do this is to pass 'this' as the parameter in the constructor of the nested class, like 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.