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();
}
});
}
}
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();
}
});
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();
}
});
}
}
You need to define that
in the outer scope.
function outerClass() {
var that = this;
// ...
$.ajax({
success: function() {
that.functionToCall();
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With