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

后端 未结 1 1193
无人共我
无人共我 2020-11-30 03:54

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:

相关标签:
1条回答
  • 2020-11-30 04:37

    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 讨论(0)
提交回复
热议问题