2

How to call method Go from inside a event handler?

function Test()
{
    this.yo = "YO";

    this.Go = function()
    {
       alert(this.yo);        
    }

    $(".aha").click(function()
    {
          //Call Go();        
    });
}

var test = new Test();

Fiddle: http://jsfiddle.net/BkNfY/9/

2
  • 2
    @TusharGupta Wow, don't impose your coding style on somebody else's question. That was a totally inappropriate edit. Commented Nov 30, 2013 at 14:19
  • another option: .click(function(){ this.Go() } .bind(this)); i like that since e.target will refer to the "old this", aka the element being clicked. you can also just do .click( this.Go.bind(this) ); Commented Nov 30, 2013 at 14:58

3 Answers 3

3

A common method is to have a local variable in your constructor that is a reference to the instance:

function Test()
{
    var self = this;

    this.yo = "YO";

    this.Go = function(){
       alert(this.yo);        
    }

    $(".aha").click(function(){
          self.Go();        
    });
}

Or you can bind the function you pass to .click():

    $(".aha").click(function(){
          this.Go();        
    }.bind(this));
Sign up to request clarification or add additional context in comments.

3 Comments

By way of explanation: this is a somewhat magical variable that cannot be closed over, so the context is lost in your click handler. You need to set it aside in a variable that can be closed over, and one of the very common ways of doing this is with a variable called self. There is nothing magic about this variable, it's just a somewhat common convention.
Isnt self itself a global context identifier?
@bergman - Um...yes. But in this case it's a local variable since it's declared inside a function with var (if needed the same function could reference the global one with window.self). Feel free to use some other name...
3

http://jsfiddle.net/BkNfY/12/

function Test(){
    var that = this;

    that.yo = "YO";

    that.go = function(){
       alert(that.yo);        
    }

    $(".aha").click(function(){
       that.go();        
    });

    return that;
}

var test = new Test();

but it would make more sense to do it like this: (if you want to reuse Test)

function Test(){
    var that = this;

    that.yo = "default YO";

    that.go = function(){
       alert(yo);        
    }

    return that;
}

var test = new Test();
test.yo = "YO";

$(".aha").click(function(){
     test.go();        
});

You could keep yo "private" if it is not going to be used outside of Test, e.g. test.yo

function Test(){
    var that = this,
        yo = "YO"; // yo is now "private"
                   // so can't modify yo from outside of this function

    that.go = function(){
       alert(yo);        
    }

    return that;
}

var test = new Test();

$(".aha").click(function(){
     test.go();        
});

5 Comments

There's no need to add the return statement. Also, why is yo suddenly a private variable?
@nnnnnn yo is still an instance variable in that it is bound to a specific instance of a Test.
@meagar - Yes, but what I meant is that you can't use test.yo. I've edited my comment to say "private" instead of "instance".
@nnnnnn No, it's "private" in that sense.
@nnnnnn "There's no need to add the return statement." - I agree, in this example not needed. Just a habbit, in case if we do e.g. that = new SuperTest(). I made yo "private" to give OP something more to think about, as he is going towards classical approach, and I thought he might find it useful. Edited, thank you.
0

The jQuery way :

$('.aha').click($.proxy(function (e) {
    // e.target refers to the clicked element
    this.Go();
}, this));

Shorter :

$('.aha').click($.proxy(this.Go, this));
$('.aha').click($.proxy(this, 'Go'));

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.