5

我意识到 TypeScript 编译器正试图忠实于普通的旧 JavaScript,因为 TypeScript 确实是 JavaScript。但是,Intellisense 解释为“this”关键字的内容与其在运行时实际解析的内容之间存在脱节。例如,考虑以下 TypeScript ajax 调用:

 getAgencies() {
            var self = this;          
            $.ajax(liveString + "/Home/GetSupportedAgencies",
            {
                type: "GET",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: Utilities.Logger.displayAjaxError,
                success: this.onGetAgenciesComplete
            });

        }

及其相应的回调:

   onGetAgenciesComplete(agencies) {
                var self = this;
                    if (agencies == null)
                        Utilities.Logger.displayErrorOnLogConsole("There was an error retrieving supported agencies.  Refresh site and try again.");
                    else {
                        $.each(agencies, function (i, a) {
                            self._indexViewModel.agencies.push({ name: a.Name, fullName: a.FullName, shortName: a.ShortName, bbox: a.BBox, countryCode: a.CountryCode });
                        });

                        if (Modernizr.geolocation) {
                            navigator.geolocation.getCurrentPosition(
                                function (position) {
                                    self.initMapPage(position, self);
                                },
                                function (error) {
                                    Utilities.Logger.displayErrorOnLogConsole("Oops, we could not get your location at his time. Please try later.");
                                });
                        }
                        else {
                            Utilities.Logger.displayErrorOnLogConsole("Sorry, your browser does not return location information.");
                            self.getBusRoutes(self.agencyName);
                        }


                        // end of initialization
                    }
                }

现在,当我将鼠标悬停在 TypeScript 源文件中 onGetAgenciesComplete 中的“var self = this”行上时,变量“self”的 Intellisense 定义表明它是 HomePageViewModelBase 类型,其中 HomePageViewModelBase 是包含上述方法的类。

上述生成的Javascript如下:

HomePageViewModelBase.prototype.getAgencies = function () {
            var self = this;
            $.ajax(liveString + "/Home/GetSupportedAgencies", {
                type: "GET",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: Utilities.Logger.displayAjaxError,
                success: this.onGetAgenciesComplete
            });
        };
        HomePageViewModelBase.prototype.onGetAgenciesComplete = function (agencies) {
            var self = this;
            if(agencies == null) {
                Utilities.Logger.displayErrorOnLogConsole("There was an error retrieving supported agencies.  Refresh site and try again.");
            } else {
                $.each(agencies, function (i, a) {
                    self._indexViewModel.agencies.push({
                        name: a.Name,
                        fullName: a.FullName,
                        shortName: a.ShortName,
                        bbox: a.BBox,
                        countryCode: a.CountryCode
                    });
                });
                if(Modernizr.geolocation) {
                    navigator.geolocation.getCurrentPosition(function (position) {
                        self.initMapPage(position, self);
                    }, function (error) {
                        Utilities.Logger.displayErrorOnLogConsole("Oops, we could not get your location at his time. Please try later.");
                    });
                } else {
                    Utilities.Logger.displayErrorOnLogConsole("Sorry, your browser does not return location information.");
                    self.getBusRoutes(self.agencyName);
                }
            }
        };

当在 HomePageViewModelBase.prototype.onGetAgenciesComplete 中执行变量“self”时,它被解析为看起来像 AjaxContext 而不是 HomePageViewModelBase 的实例。这是预期的行为还是我应该将其报告为错误?

4

2 回答 2

6

是的,您可能应该将其作为错误报告给他们,因为thisinside an$.ajax()是指$.ajax()对象本身。

如果您想绕过它以使其正常工作,请将您的成功功能更改为:

success: self.onGetAgenciesComplete

或者,如果您希望代表this您的班级,只需使用$.ajax的上下文方法

$.ajax({ 
    context: this,
    // now *this* will come from the previous scope,
    // which in your case, is your class "HomePageViewModelBase"

    // now that we know for certain what -this- refers to
    success: this.onGetAgenciesComplete
});
于 2012-10-31T13:52:08.230 回答
3

就任何(包括 Intellisense)静态分析而言,onGetAgenciesComplete is HomePageViewModelBase和 always will be 的上下文;除了在运行时,上下文将通过显式的内部 jQuery.ajax 上下文绑定动态设置。Intellisense 可以确定上下文将动态更改的唯一方法是实际执行该代码路径,但即使这样也会导致歧义:哪个上下文是正确的上下文?

如果您在其他地方“借用”一种方法来进行一次性调用怎么办?Intellisense 是否应该使用该调用站点来确定上下文?可能不是...

HomePage.prototype.onGetAgencies.call({ some other context }, ...);

(为便于阅读而缩短。)

于 2012-11-01T17:04:21.600 回答