1

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 Answers 3

5
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));
};  
Sign up to request clarification or add additional context in comments.

2 Comments

I think "self" is a better name for variables used in this way, as it is what some other languages use instead of "this" and so more synonymous.
Using self could also lead to confusion for the same reason, as people new to JS might think it's an actual keyword.
1

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.

Comments

0
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.