0

Why doesn't this work?

function thing() {

    var bigvar;

    function method1() {
        bigvar = 1;
    }

    function method2() {
        alert(bigvar);
    }

    this.method1 = method1;
}

var a = new thing();
a.method1();
a.method2();
​

I want method2 to work, but it doesn't .. is there a way to make this work?

3 Answers 3

3

You did not make method2 public like method1 is.

this.method1 = method1;
this.method2 = method2;  //<-- missing this
Sign up to request clarification or add additional context in comments.

1 Comment

to be strict, method1 is not made public, it is just declared as an assignment to local (or global, depends on implementation) function which haphazardly have "the same" name.
0

Why do you have this.method1 = method1 but not this.method2 = method2? Try that.

Comments

0

Why not do this?

function thing() {

    var bigvar;

    this.method1 = function () {
        bigvar = 1;
    }

    this.method2 = function () {
        alert(bigvar);
    }

}

var a = new thing();
a.method1();
a.method2();​

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.