tags:

views:

37

answers:

1

Seems like a simple problem, but can't get this to work.

In the example below, unselect is called but the public variable 'this.backSelected' is undefined. If I move the code of the unselect method directly into the public off method it works.

how would I check a public variable in a private method? I don't understand why this is nor working.

Thanks.

function MyClass() 
{
    // public vars
    this.isActive = false;
    this.backSelected = false;

    // public methods
    this.on = function() {
       this.isActive = true;
       this.backSelected = true;
       // set back button on image
    }

    this.off = function() {
      this.isActive = false;
      unselect();
    }

    // private methods
    function unselect() {
        if(this.backSelected) {
           // set back button off image
        }
    };


}

var obj = new MyClass(); obj.on(); obj.off();

+5  A: 

You aren't calling unselect in context, so this doesn't mean what you think it means.

unselect.apply(this);
David Dorward
Think that's spot on.See "Privileged" section for reference: http://www.crockford.com/javascript/private.html
Gary
Thanks! Never seen the apply method before.@Gary - thanks for the link as well, great read.
Aros