开发者

JavaScript class - trouble accessing public var in private method

开发者 https://www.devze.com 2023-01-12 14:08 出处:网络
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 metho

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();


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

unselect.apply(this);
0

精彩评论

暂无评论...
验证码 换一张
取 消