maksbd19
maksbd19

Reputation: 3830

Javascript: what does this syntax mean (0,functionName)(functionParemeter);

I was wondering into the javascript file from the source of http://www.google.com actually i do it often and try to understand what they have done there. today I was wondering inside the files and found some strange function calls. Maybe its a silly thing but I really have no idea what is it and so that I couldn't help by searching for it.

a readable resemble of the code-

var someFunction = function(somaeParamenter){
    //do some stuffs;
    return something;
}

var someOtherThing = (0, someFunction)(oneParameter);

Please excuse my lack of knowledge.

EDIT:

Source-

i'm using chrome. while in the http://www.google.com page open, i opened the developer tool. then i opened the sources tab and opened the file https://www.google.com.bd/xjs/_/js/s/c,sb,cr,cdos,vm,tbui,mb,wobnm,cfm,abd,bihu,kp,lu,m,tnv,amcl,erh,hv,lc,ob,r,rsn,sf,sfa,shb,srl,tbpr,hsm,j,p,pcc,csi/rt=j/ver=WUW4ydIf-wI.en_US./am=gA/d=1/sv=1/rs=AItRSTPu52CumknQsh0was81vrM4inla_w in viewer. this file is the only js file i've seen there. I enabled the "pretty print" and in line 58 you'll find the defination-

_.Va = function(a) {
            var b = typeof a;
            if ("object" == b)
                if (a) {
                    if (a instanceof window.Array)
                        return "array";
                    if (a instanceof window.Object)
                        return b;
                    var c = window.Object.prototype.toString.call(a);
                    if ("[object Window]" == c)
                        return "object";
                    if ("[object Array]" == c || "number" == typeof a.length && "undefined" != typeof a.splice && "undefined" != typeof a.propertyIsEnumerable && !a.propertyIsEnumerable("splice"))
                        return "array";
                    if ("[object Function]" == c || "undefined" != typeof a.call && "undefined" != typeof a.propertyIsEnumerable && !a.propertyIsEnumerable("call"))
                        return "function"
                } else
                    return "null";
            else if ("function" == b && "undefined" == typeof a.call)
                return "object";
            return b
        };

and in line 83 you'll see the function is called.

_.Za = function(a) {
            return "array" == (0, _.Va)(a)
        };

Upvotes: 6

Views: 416

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382092

(0, someFunction)

simply returns someFunction

so this is just equivalent to

var someOtherThing = someFunction(oneParameter);

Are you sure you typed it exactly as it was ? If so, and if it wasn't some kind of obfuscation, then it might be the unfortunate result of some minification. If the real code were a little different, for example (0, someObject.someFunction) , there might be some use of this indirect function call.

EDIT :

You edit confirms that it the goal was probably to ensure that this, inside the function, is the global object (window in a browser) and not the object on which Va was attached (_).

Upvotes: 7

Related Questions