1

I am creating a object in javascript like this

function myobject() {
   this.myvar1 = 0;
   this.myvar2 = 0;
}

myobject.prototype.a = function(){
    this.myvar1 +=1;
    $('#button').click(function () {  // 'this' is undefined
        alert(this.myvar1)
     })
}

var mything = new myobject();
mything.a()

What is the proper way to pass the this pointer to an anonymous function?

3
  • 1
    The code as written ought to work. Let's see exactly how you're calling this.b() Commented Nov 30, 2010 at 0:23
  • 1
    works for me: jsfiddle.net/ygkBP Commented Nov 30, 2010 at 0:26
  • btw, consider capitalizing constructor function names: javascript.crockford.com/code.html#names Commented Nov 30, 2010 at 2:37

5 Answers 5

4

For updated question: The issue is that inside that event handler, this refers to the element you clicked on, rather than your myobject, so just keep a reference to it, like this:

myobject.prototype.a = function(){
    this.myvar1 +=1;
    var self = this;
    $('#button').click(function () {
        alert(self.myvar1)
    });
}

You can test it out here.


For previous question: What you have should work (exactly as you have it), you can test it here.

Sign up to request clarification or add additional context in comments.

2 Comments

I oversimplified the example. I updated the code. I think the problem has to do with passing the 'this' pointer to an anonymous function.
@Alexis - It's not that this is undefined, it just refers to the handler inside that function, just maintain a reference like this: jsfiddle.net/nick_craver/6Ltac
1

Adding the following code to the end of your snippet and running it correctly alerts '1':

mything.b();
alert(mything.myvar1);

What were you expecting? Perhaps you're incorrectly invoking mything.b() ?

1 Comment

you're right. In the example, I over simplified the code. I updated the example.
1

To pass this to an inner function, "save" it to another variable.

myobject.prototype.a = function() {
    var self = this;
    self.myvar1 += 1;

    $('#button').click(function () {
         alert(self.myvar1);
    });
}

Comments

0

What's happening here is that jQuery is hijacking the value of this in the anonymous function used as a handler for the click event. It's kind of confusing, but jQuery's behavior is to assign the DOM element that triggered the event to this. There are a couple ways to set the value of this explicitly (the "context") for the callback function:

  1. Function.prototype.bind - Part of the ECMAScript 5 standard, just starting to be implemented in browsers. For legacy support, you can extract it from the Prototype library.
  2. $.proxy - jQuery's crappy equivalent of Function.prototype.bind, which was introduced in jQuery 1.4.

I wrote a couple posts on my blog that explain these in more detail if you're interested:

In both cases, since this will now be your object and not the triggering DOM element, you can get the triggering DOM element by inspecting the event object, which will be the second parameter to your callback function.

1 Comment

this is a very useful answer also!
0

Others here have done a nice job explaining the issue and how to resolve without adding any dependencies, but personally I think this plugin is a better overall solution (if you don't mind the dependency):

http://markdalgleish.com/projects/eventralize/

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.