在javascript上使用这个而不是上下文变量有什么缺点吗



我正在与其他开发人员一起进行Aurelia项目

他们中的一些人正在使用this并且一些正在声明上下文变量

我想知道的是,Javascript的使用是否存在性能差异、内存分配或任何标准。


这个 的例子:

    activate() {
            this.isLoading = true;
            this.modeText = "Edit";
            this.service.getX(this.appState.StoreId,this.variableX).then(response => {
                let stuff = JSON.parse(response.response);
                this.widget = stuff;
                this.isLoading = false;
            });
}

没有这个的例子:

    activate() {    
            var context = this;
            context.isLoading = true;
            context.modeText = "Edit";
            context.service.getX(context.appState.StoreId,context.variableX).then(response => {
                let stuff = JSON.parse(response.response);
                context.widget = stuff;
                context.isLoading = false;
            });
}

基本上,对于精简函数或低级别嵌套函数(作用域链接)没有区别。但为了避免应用程序增长时性能低下,建议使用context。如果可以的话,请阅读高性能Javascript,N.Zakas的第2章,其中有一个完整解释的案例,并附有性能图。

最新更新