Umfang in einem $.ajax-callback-Funktion

Wenn ich mit diesem code, warum hat sich der Rahmen geändert für die "doStuff" - Alarm? Gibt es eine Möglichkeit, dass ich sicherstellen kann, dass der Geltungsbereich ist meine Sache und nicht das Window-Objekt?

Hier ist der gleiche 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);​
InformationsquelleAutor bconrad | 2012-11-02
Schreibe einen Kommentar