JavaScript class - trouble accessing public var in private method
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);
精彩评论