What is meant by (0, somefunc) (args)

I was looking through some of gmails javascript (writing an extension), and I saw (0, _.ab)(a) or options in all places. What does it mean?

I tried some tests like

 function a(a,b,c){ console.dir(a+b+c); } (0, a)(1,2,3) 

However, I cannot understand why they simply will not name a(1,2,3) directly. Does this give an odd advantage when using (0,a) ?

I did jsperf ( http://jsperf.com/direct-vs-0-func-calls ) to test this, and a(1) vs (0,a)(1) seem identical.

Edit: as far as I can make out Google, use it only where they need to call the function directly, for example if ((0, _.wa)(a)) (taken from gmail source)

+7
javascript function
source share
1 answer

The syntax (0, func)() ensures that the ( this ) context in the func function being called is the global context.

For example:

 var myContext = { func: function () { console.log(this); } }; myContext.func(); // => myContext (0, myContext.func)(); // => window 
+11
source share

All Articles