23

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?

lyrically wicked
  • 1,185
  • 12
  • 26

1 Answers1

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
Sudhansu Choudhary
  • 3,322
  • 3
  • 19
  • 30
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    It looks like this answer differs from that duplicate... But where exactly is the documentation describing this behavior? – lyrically wicked Oct 23 '13 at 07:57
  • 2
    Keep in mind that this code is probably generated from another source, so I guess it's a result of an optimizer/minifier/whatever and probably not written that way by programmers. – pawel Oct 23 '13 at 07:58
  • That, and `0` could pretty much be replaced with anything right? `(this, this.b)();` – MackieeE Oct 23 '13 at 08:00
  • 1
    @MackieeE yes. Sorry, I don't have time to comment more right now, I'm in a meeting... – Denys Séguret Oct 23 '13 at 08:12
  • Note that indirect calls are also useful to escape ES5 "strict" mode. In fact, it's the official mechanism to run non-strict code within strict mode. – slebetman Oct 23 '13 at 08:34
  • @dystroy Thank you dystroy & no problem =) I know it's obfuscated code so variables and values are quite mixed/cryptic; but I just wonder the significance of the `0` or what not! – MackieeE Oct 23 '13 at 08:56
  • 1
    @MackieeE It's probably the same before encryption. Personally I also use `0` when the value doesn't matter, as in `Array.apply(0,Array(10)).map(Math.random)`. – Denys Séguret Oct 23 '13 at 09:03
  • I got it: [indirect-function-call-in-javascript](http://stackoverflow.com/questions/5161502/indirect-function-call-in-javascript) – lyrically wicked Oct 23 '13 at 09:51
  • 1
    @Anony-Mousse Well... You don't often use them... It's a little messy but it's one of the most expressive languages today. I can make in two pages of node.js programs I'd make in four pages of go and more than ten pages of java (I won't even speak of PHP). – Denys Séguret Oct 23 '13 at 11:20
  • 3
    @Anony-Mousse Some people value reliability, other people value simplicity of expression, yet more people value both. The fact that javascript isn't as reliable as java doesn't prevent it from being more expressive. They're on orthogonal axies. However being a more expressive language does allow it to be used in a way that is considerably less *ugly* and *messy*. – Racheet Oct 23 '13 at 11:44