Skip to content Skip to sidebar Skip to footer

Purpose Of (0, Obj.method)(param1, Param2) In Closure Compiler Minified Code

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.

Solution 1:

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

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.

Post a Comment for "Purpose Of (0, Obj.method)(param1, Param2) In Closure Compiler Minified Code"