I have a class like this :
var MyClass = function(){
this.field = 'field';
}
MyClass.prototype.doSth(data){
//doSth with data and this.field;
}
MyClass.prototype.getData(){
$.ajax({
type: "post",
url: 'myurl',
}).success(this.doSth);
}
but in doSth all ‘this’ point to the jquery ajax object not MyClass instance.
I added a static filed _self to point MyClass self MyClass._self = this; then change all the this to MyClass._self in doSth can fix. But I think this is ugly.
I want to know is there any way to solve my problem with out modify doSth body?
You can pass an object using the context parameter to be used int he callbacks.