最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin8浏览0评论

Sometimes I stared at js provided with google 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?

Sometimes I stared at js provided with google. 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?

Share Improve this question edited Oct 24, 2013 at 4:14 lyrically wicked asked Oct 23, 2013 at 7:36 lyrically wickedlyrically wicked 1,41713 silver badges29 bronze badges 8
  • 4 possible duplicate of Javascript syntax (0, fn)(args) – JJJ Commented Oct 23, 2013 at 7:43
  • 6 @Juhana but this time there's an actual answer, not just explanation of ma operator ;) – pawel Commented Oct 23, 2013 at 7:55
  • 1 That is true. I kinda wish dystroy would have answered that question instead. – JJJ Commented Oct 23, 2013 at 7:56
  • 3 Thanks to dystroy, I would rather mark that question as a duplicate to this one... – Passerby Commented Oct 23, 2013 at 8:00
  • 5 Actually, all those other questions should be flagged as a duplicate of THIS question since this question has the correct answer. – slebetman Commented Oct 23, 2013 at 8:32
 |  Show 3 more ments

1 Answer 1

Reset to default 40

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
发布评论

评论列表(0)

  1. 暂无评论