6

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?

Stephen Touset
  • 2,562
  • 2
  • 25
  • 24
  • The compiler is forcing the second argument to the comma operator to be interpreted as an expression. If instead of `_.Q` we had an anonymous function, it would be invoked (instead of throwing an error). This is the only reason for this that I can think of. – bfavaretto Sep 05 '13 at 01:32
  • Can you give an example? – Stephen Touset Sep 05 '13 at 05:15
  • I meant `(0, function(a,b){})("gapi.auth.authorize", _.Ek.Ff)` is okay, but `function(a,b){}("gapi.auth.authorize", _.Ek.Ff)` is not (syntax error). So the compiler might be adding that to be safe, depends on what goes after the zero. In any case, the zero wouldn't be necessary at all, so now I have another guess: maybe it's possible for the compiler to put `eval` instead of `_.Q`; in this case, `(0,eval)` would force an indirect eval call (or "global eval"). See http://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript. – bfavaretto Sep 05 '13 at 17:57

1 Answers1

5

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.

John
  • 5,443
  • 15
  • 21