javascriptgoogle-mapsinfobubble

Why does the InfoBubble.js author reassign protoype methods with bracket notation?


From Google Maps Utility Library looking at the source of InfoBubble I found that the author creates prototype methods with dot notation, but then at the end of the method definition he reassigns the same proto property with bracket notation.

This should clarify:

/**
 * Set the style of the shadow
 *
 * @param {number} shadowStyle The style of the shadow.
 */
InfoBubble.prototype.setShadowStyle = function(shadowStyle) {
  this.set('shadowStyle', shadowStyle);
};
InfoBubble.prototype['setShadowStyle'] = InfoBubble.prototype.setShadowStyle;

Any idea?


Solution

  • I think I've worked it out.

    This apparent nonsense appears to be to do with Google Closure compilation.

    /**
     * Set the style of the shadow
     *
     * @param {number} shadowStyle The style of the shadow.
     */
    InfoBubble.prototype.setShadowStyle = function(shadowStyle) {
      this.set('shadowStyle', shadowStyle);
    };
    InfoBubble.prototype['setShadowStyle'] = InfoBubble.prototype.setShadowStyle;
    

    Compiles to :

    k.prototype.ma=function(a){this.set("shadowStyle",a)};
    k.prototype.setShadowStyle=k.prototype.ma;
    

    As you can see, the dot-notation .setShadowStyle gets minified to .ma, allowing internal calls to be as concise as possible by using the minified form.

    But since this is a Public method, it's necessary to provide a means of calling the method by its original name. This is achieved by having the compiler minimize only dot-notation and not associative-notation.

    Thus, everybody is happy; internal minification and external accessibility.

    What I can't explain is why the compiler can't simply work out for itself that it needs to preserve the original name for Public use. As far as I can see, it could do so by detecting the absence of the @private tag in the method's preamble block.

    Maybe :

    Who knows which?