9

So I have some javascript class and in one method I use jQuery to bind function to click event. And within this function I need to call other methods of this class. In usual js function I did it through "this.method_name()", but here, I guess, jQuery redefines "this" pointer.

Anurag
142k37 gold badges223 silver badges262 bronze badges
asked Jul 29, 2010 at 16:47
1
  • 1
    Perhaps it would help if you pasted a short code snippet. Commented Jul 29, 2010 at 16:49

3 Answers 3

21

jQuery doesn't redefine the this pointer, but that's how JavaScript functions work in general. Store a reference to the this pointer under a different name, and use that.

var self = this;
$("selector").click(function() {
 self.method_name();
});

See this answer for more approaches.

answered Jul 29, 2010 at 16:50
Sign up to request clarification or add additional context in comments.

2 Comments

@Tramp - you're welcome. But note that this is still a hacky approach. Instead use jQuery.proxy, or even better the bind method that is now included in the standard. If it's not available in some browser, it's easy to define one - stackoverflow.com/questions/3018943/…
Maybe this the "best approach" but I think its the most readable and less code.
3

There are a few different ways to do this.

Anurag has a perfect example of one.

Two other ways are the jQuery Proxy class (Mentioned in other answers) and the 'apply' function

Now lets create an object with click events:

var MyObj = function(){
this.property1 = "StringProp";
// jQuery Proxy Function
$(".selector").click($.proxy(function(){
 //Will alert "StringProp"
 alert(this.property1);
// set the 'this' object in the function to the MyObj instance
},this));
//Apply Function
//args are optional
this.clickFunction = function(arg1){
 alert(this.property1);
};
$(".selector").click(this.clickFunction.apply(this,"this is optional"));
};
answered Jul 30, 2010 at 0:12

Comments

2

In addition to the possibility of temporarily storing a reference to this (self = this, see Anurag's answer), since ES6 it is possible to use arrow functions for this problem. These have no "own" this. This means that the "usual" object-related this can be accessed again within an arrow function within an event handler:

$("selector").click(() => {
 this.method_name();
});

Further information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions?retiredLocale=de#cannot_be_used_as_methods

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions?retiredLocale=de#using_call_bind_and_apply

answered Dec 2, 2022 at 9:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.