Here is an abstract JavaScript code sample that illustrates the situation that is causing me to ask a question here:
function className (){
var private_variable = 0;
function privateMethod(){
// Some irrelevant code.
};
this.privilegedMethod = function (){
// Some relevant code to determine if private variable needs to be modified.
private_variable+=val; // Modifies private variable.
};
this.init = function () {
$(window).keydown(function (key) {
if (key.which == 13) {
privateMethod(); // Call to private method works fine.
this.privilegedMethod(); // 'this' references Window object,
// not this class method as expected.
}
});
};
};
My question is - is there an alternative way I can make call this.privilegedMethod() reference to it's class, not the Window object which it is applied to?
Or perhaps any suggestions how I could restructure my code keeping the functionality -- key events are listened globally, the method of modifying the private variable is accessible outside the class, but private variable itself is not.
P.S. Placing the call to the privileged method inside the private did not change anything.
this.init = function () {
var that = this;
$(window).keydown(function (key) {
if (key.which == 13) {
privateMethod();
that.privilegedMethod();
}
});
};
or
this.init = function () {
$(window).keydown($.proxy(function (key) {
if (key.which == 13) {
privateMethod();
this.privilegedMethod();
}
}, this));
};