2

As far as I know, the dot and bracket notation in Javascript have the exact same effects (other than accessing properties with special names). I was reading this piece of code in the Google Maps utilities library and I came across this line (123):

MapLabel.prototype['onAdd'] = MapLabel.prototype.onAdd;

What is the significance of this line? Does it have any actual effects?

Community
  • 1
  • 1
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 2
    I can only imagine that they are doing this for the/a minifier. The bracket notation will preserve the name for the user facing API, but will rename the function internally (i.e. all occurrences of `onAdd` will probably be changed to a shorter name). – Felix Kling May 24 '16 at 00:37
  • @FelixKling - Ah, I didn't think of that and that is probably the right reason why they did it. – Derek 朕會功夫 May 24 '16 at 00:38
  • 1
    OTOH, that method isn't called in the file itself ;) So in addition to that it might just be style guidelines. – Felix Kling May 24 '16 at 00:39
  • It also seems to be only doing it for the `public` methods, so I think the minifier theory is a good one – jasonscript May 24 '16 at 01:23
  • 2
    Probably for Google's [Closure Compiler](https://developers.google.com/closure/compiler/). Non-quoted properties get mangled and unused code gets removed, so exported properties need to be quoted to indicate that they need to stay and remain the same. The rest of the annotations in the documentation comments look like CC too. –  May 24 '16 at 01:30
  • Indeed it's [GCC](https://developers.google.com/closure/compiler/docs/api-tutorial3#separately). Related: [JSLint Weird assignment](http://stackoverflow.com/q/25002331/1048572) – Bergi May 24 '16 at 02:33

0 Answers0