6

I have an issue with OO Javascript and a jQuery callback. If you look at the sample below it should explain everything.

How do I call functionToCall() deep within this functception.

function outerClass() {
    this.functionToCall = function() {
        //do something
    }

    this.someOtherFunction = function() {

    this.aCoupleOfVariables1 = 2;
    this.aCoupleOfVariables2 = "stuff";

    $.ajax({
        success: function() {
        //How do I call functionToCall() right here
        //TRIED:
            functionToCall();
            this.functionToCall();
            that.functionToCall();
        }
    }); 
    }
}

5 Answers 5

14

You can pass this as the context option to $.ajax():

$.ajax({
    context: this,
    success: function() {
        // Here, 'this' refers to the same object as in the caller.
        this.functionToCall();  
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Ooo, I like this so much more.
Just a sidenote for the OP: whenever you need to make use of this and can't use context or any other property for the matter (lots of cases), api.jquery.com/jQuery.proxy
Nice, this avoids having to use $.proxy as well.
6

Have a local reference to it,

function outerClass() {

    var self = this;

    this.functionToCall = function() {
    //do something
    }

    this.someOtherFunction = function() {

    this.aCoupleOfVariables1 = 2;
    this.aCoupleOfVariables2 = "stuff";

    $.ajax({
        success: function() {
           self.functionToCall();
       }
    }); 
   }
}

Comments

5

You need to define that in the outer scope.

function outerClass() {
    var that = this;

    // ...

     $.ajax({
        success: function() {
            that.functionToCall();
        }
    }); 
}

Comments

4

You need to store the reference for the this value from the parent scope:

var parentScope = this;

and then access functionToCall via that object

parentScope.functionToCall();

Example:

function outerClass() {
    var parentScope = this;

    this.functionToCall = function() {
        //do something
    }

    // ...

    $.ajax({
       success: function() {
           parentScope.functionToCall();
       }
    });
}

Another way to accomplish that would be to use Es5's .bind() to set the value for this within your inner function(-context)

$.ajax({
    success: function() {
        // Here, 'this' refers to the same object as in the caller.
        this.functionToCall();  
    }.bind(this)
});

Comments

2

Maintain the scope of the function in a local variable and use it or you can also use jquery proxy in which you can specify the context.

function outerClass() {
    this.functionToCall = function() {
        //do something
    }

    this.someOtherFunction = function() {

    this.aCoupleOfVariables1 = 2;
    this.aCoupleOfVariables2 = "stuff";


    $.ajax({
        success: $.proxy(function() {
            this.functionToCall();
        }, this)
    }); 
    }
}

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.