If I am using this code why has the scope changed for the "doStuff" alert? Is there a way that I can make sure that the scope is my object and not the Window object?
Here is the same code in jsfiddle.
(function ($) {
var c$ = {};
c$.TestScope = function () {
this.doAjax = function (onComplete) {
var self = this;
$.ajax({
url: 'badurl',
complete: function (data) {
alert('doAjax2 self === c$.oTestScope: ' + (self === c$.oTestScope).toString());
onComplete(data);
}
});
alert('doAjax1 self === c$.oTestScope: ' + (self === c$.oTestScope).toString());
};
this.doStuff = function (data) {
var self = this;
alert('doStuff self === c$.oTestScope: ' + (self === c$.oTestScope).toString());
}
};
c$.oTestScope = new c$.TestScope();
c$.oTestScope.doAjax(c$.oTestScope.doStuff);
})(jQuery);
I have altered your code to pass a reference to
this
onto the doStuff() code.You should be able to specify the
this
value as the context in your$.ajax()
parameters:Edit I made a fiddle for this and verified that it works properly. There's no messing around with an extra
self
parameter or having to muck around with closures to keep your variables.Part of what you were missing was calling
onComplete.call(this, data)
to invokedoStuff()
.