views:

27

answers:

3

I have defined a class named MyClass and I have defined two methods myMethod1 and myMethod2 for it:

function MyClass() {}
MyClass.prototype.myMethod1 = function() {...};
MyClass.prototype.myMethod2 = function() {...};

Inside myMethod1, I use jQuery and there's a callback closure defined there:

MyClass.prototype.myMethod2 = function() {
 $.jQuery({success: function(data) {
  this.myMethod2();
 }, ...});
}

Now the problem is that this no longer is referring to MyClass. The question is how can I refer to it? At the moment I have assigned it to a variable named thisObj and access it this way:

MyClass.prototype.myMethod2 = function() {
 var thisObj = this;
 $.jQuery({success: function(data) {
  thisObj.myMethod2();
 }, ...});
}

Is there a better way to access MyClass.this from the closure nested in myMethod2?

Thanks in advance.

+1  A: 

Your solution is perfectly fine. Since you already have a closure there, may as well make use of it, that's absolutely fine.

But if you like, you can use jQuery.proxy instead, like this:

MyClass.prototype.myMethod2 = function() {

    $.jQuery({success: jQuery.proxy(function(data) {
        this.myMethod2();
    }, this), ...});
}

Again, though, there's nothing wrong with your original solution. Using proxy can be helpful, though, when you want to reuse a function in lots of different places, or when you don't already have a closure and don't want to introduce one (perhaps because it would close over a lot of unrelated stuff). The good thing about proxy is that it creates a closure over a controlled set of stuff and not over the current scope.

T.J. Crowder
+1  A: 

You can pass a reference to this to the function, but your solution seems fine to me. You are just using the lexical scoping rules of Javascript so what is wrong?

Gabor de Mooij
+1  A: 

The method you've used is often called the "that reference", because the name that is commonly used as a name for the copy of the this reference. See Crockford's talks on JavaScript for example.

Daniel Earwicker
Or `self`. I can't *stand* `that`, it's completely misleading. :-)
T.J. Crowder
Bytecode Ninja
Guys both of your answers deserve a check mark, however as I can't do that, I check mark the first answer, and up vote both of them.
Bytecode Ninja
@bytecode: Right thing to do going with the first right answer. :-) Re naming, both `self` and `that` are widely used. `self` has a long pedigree, it's the `this` of [Smalltalk](http://en.wikipedia.org/wiki/Smalltalk), and Smalltalk is an ancestor of JavaScript's, indirectly through the language [Self](http://en.wikipedia.org/wiki/Self_programming_language). I've also seen `me`, presumably as an homage to Visual Basic.
T.J. Crowder
@T.J: The thing is, when using `self`, `that`, or `me` inside the closure, it is not really referencing to `this` (i.e. the closure) anymore and it is actually referring to its parent context. On the other hand, if you define the reference as `parent`, in the place where you define it, it is not really the parent but `this` itself and we have some kind of paradox here! :) Having said that as I've seen `self` being used in a couple of other languages, and as you said in Smalltalk too, I am also mentally more comfortable with `self`.
Bytecode Ninja