6

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)
        };
maksbd19
  • 3,785
  • 28
  • 37
  • 1
    Can you cite the exact source, please? I cannot find it in what I get delivered at `http://www.google.com/` – Bergi May 04 '13 at 16:49
  • 1
    Side note: Google's JS is heavily optimized and minified so it's probably not the best place to take influences from. – JJJ May 04 '13 at 16:55
  • @Juhana you are right but i can't resist myself from peeking there :) – maksbd19 May 04 '13 at 17:57
  • @bergi i've edited my question citing the source. – maksbd19 May 04 '13 at 17:58
  • is this simply a comma operator? https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Expressions_and_Operators#Comma_operator – maksbd19 May 04 '13 at 18:17
  • @maksbd19: Thanks! Yes, it's the comma operator, and it seems pretty useless here. – Bergi May 04 '13 at 19:40

1 Answers1

7
(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 (_).

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Instead of `0`, they probably had an assignment or function call or any other expression. – Zirak May 04 '13 at 16:25
  • `(Potato, I think) it is the result of obfuscation.` – tckmn May 04 '13 at 16:26
  • 3
    There is one delicate exception where they are not equal: [`(0, eval)(…)`](http://perfectionkills.com/global-eval-what-are-the-options/#evaling_in_global_scope) – Bergi May 04 '13 at 16:46
  • @Bergi That's an interesting point. Calling the function indirectly maybe was the goal here but I don't see how it could be useful with the code we see. – Denys Séguret May 04 '13 at 17:14