Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does google main page use (0, obj.func)(args) syntax?

Tags:

javascript

Sometimes I stared at js provided with google.com main page and found that they tended to use (0, obj.func)(args) syntax. Here are excerpts from the script:

 var _ = _ || {};
 (function (_) {
     var window = this;
     try {
         _.mb = function (a) {
             return (0, window.decodeURIComponent)(a.replace(/\+/g, " "))
         };
         _.zg = function (a, b) {
             for (var c = a.length ? a.split("&") : [], d = 0; d < c.length; d++) {
                 var e = c[d];
                 if ((0, _.Ag)(e) == b) return (c = /=(.*)$/.exec(e)) ? (0, _.mb)(c[1]) : null
             }
             return null
         };
         _.Ag = function (a) {
             return (a = /^(.+?)(?:=|$)/.exec(a)) ? (0, _.mb)(a[1]) : null
         };
         var Cg = function (a, b) {
                 var c = a.indexOf("?");
                 return 0 > c ? null : (0, _.zg)(a.substring(c + 1), b)
             };
         // Note var Cg called with no 0
         var oca = function (a) {
                 this.A = Cg(a, "mods");
                 this.B = Cg(a, "ver")
             };
     } catch (e) {}
 })(_);  

Why prepending 0?

like image 209
lyrically wicked Avatar asked Oct 23 '13 07:10

lyrically wicked


1 Answers

This makes an indirect call.

This ensures the context, in the called function, is the global one. This might be useful in an internal scope.

Example :

var a = {
  b: function(){
     console.log(this);    
  },
  c1: function(){
     this.b(); 
  },
  c2: function(){
     (0, this.b)(); 
  },
  c3: function(){
     (this.b)(); 
  }
}
a.c1(); // logs a
a.c2(); // logs window
a.c3(); // logs a
like image 50
Denys Séguret Avatar answered Oct 17 '22 08:10

Denys Séguret