While investigating google plusone scripts, I’ve seen following syntax many times:
(0, _.Em)();
Assuming _.Em is a function the statement above would result in calling that function, that’s pretty obvious. If, on the other hand, it would be undefined, wouldn’t the result be the same as doing simply _.Em() ?
Can anyone shed a light on what’s idea behind using such syntax?
Basically, this syntax allows to call
_.Em()in the context of thewindowobject instead of_.Assuming you have this code:
Issuing
_.Em()will result inEm()being called in the context of_. Inside the function, thethiskeyword will refer to_, sofoowill be printed.Issuing
(0, _.Em)()decouples the method call from the object and performs the call in the global context. Inside the function, thethiskeyword will refer towindow, soundefinedwill be printed, sincewindowdoes not have afooproperty.You can test the difference between the two syntaxes in this fiddle.