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);