Closure Compiler缩小代码中的(0,obj.method)(param1,param2)的用途 [英] Purpose of (0, obj.method)(param1, param2) in Closure Compiler minified code

查看:90
本文介绍了Closure Compiler缩小代码中的(0,obj.method)(param1,param2)的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种方法是什么?例如,来自Google OAuth API:

 (0,_。Q)(gapi.auth.authorize,_ .Ek.Ff); 
(0,_.Q)(gapi.auth.checkSessionState,_。Ek.MH);
(0,_.Q)(gapi.auth.getAuthHeaderValueForFirstParty,_。Ek.Qe);
(0,_.Q)(gapi.auth.getToken,_。Ek。$ f);
(0,_.Q)(gapi.auth.getVersionInfo,_。Ek.Wk);
(0,_.Q)(gapi.auth.init,_。Ek.gb);
(0,_.Q)(gapi.auth.setToken,_。Ek.Ym);

对我而言,这似乎只是输出

  _。Q(gapi.auth.authorize,_。Ek.Ff); 
_.Q(gapi.auth.checkSessionState,_ Ek.MH);
...

我假设不是。那有什么区别?

解决方案

编译器确保this值是正确的:

  af()//'此'值为a
(0,af)()//'此'为默认值this 值

您在OAuth API中看到这一点的原因是代码使用的是rescope全局符号 编译通过。此过程将放置在全局范围内的符号放置在跨功能范围(IIFE)的对象上进行通信。所以这样的代码:

  function f(); 

//一些可能迟到的代码
f();

变为:

 (function(_){
_.f = function(){};
})(某事);

(函数(_){
_.f();
})(某事);

但是这里f的'this'值已从默认'this'更改为_。为了防止发生这种变化,使用(0,_.f)()代替。



这是编译器可以改进的一个区域,因为即使在它可以确定函数体中没有使用this的情况下,它也会这样做。 / p>

What is this approach for? For instance, from the Google OAuth API:

(0, _.Q)("gapi.auth.authorize", _.Ek.Ff);
(0, _.Q)("gapi.auth.checkSessionState", _.Ek.MH);
(0, _.Q)("gapi.auth.getAuthHeaderValueForFirstParty", _.Ek.Qe);
(0, _.Q)("gapi.auth.getToken", _.Ek.$f);
(0, _.Q)("gapi.auth.getVersionInfo", _.Ek.Wk);
(0, _.Q)("gapi.auth.init", _.Ek.gb);
(0, _.Q)("gapi.auth.setToken", _.Ek.Ym);

To me, this would seem to be identical to simply outputting

_.Q("gapi.auth.authorize", _.Ek.Ff);
_.Q("gapi.auth.checkSessionState", _Ek.MH);
...

I'm assuming it isn't. so what's the difference?

解决方案

The compiler is ensuring the "this" value is correct:

a.f()  // 'this' value is "a"
(0, a.f)()  // 'this' is the default "this" value

The reason you see this in the OAuth API is the code is using the "rescope global symbols" compiler pass. This pass places symbols that would otherwise be introduced into global scope to communicate across function scopes (IIFEs) onto a object. So code like this:

function f();

// some potentially late loaded code
f();

becomes:

(function(_){
  _.f = function() {};
})(something);

(function(_){
  _.f();
})(something);

But here "f"'s 'this' value has changed from the default 'this' to "_". To prevent that change from happening, "(0, _.f)()" is used instead.

This is an area where the compiler could improve because it does this even in cases where it can determine that "this" is not used in the body of the function.

这篇关于Closure Compiler缩小代码中的(0,obj.method)(param1,param2)的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆