JavaScript: call of function with 'this.' does not reference to method in class

739 Views Asked by At

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.

3

There are 3 best solutions below

2
On BEST ANSWER
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));
};  
0
On

The deal is that inside the keydown handler the scope is the window object, so the "this" keyword refers to the window which does not have you method.

James solution should work fine.

0
On
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 () {
        var handle = $.proxy(this.privilegedMethod, this);
        $(window).keydown(function (key) {
            if (key.which == 13) {
                privateMethod();    
                handle();                                                 
            }
        });
    };  
};

http://api.jquery.com/jQuery.proxy/