3

I have to make sense of a codebase I was given on my new job. I can see many anti-patterns here, one of them is a "god object", which contains a lot of things and different object access it all the time. That's a different problem, my question here is about the fact, that some objects access its members using '.', others do it via [], for example

GOD.meow.woof()

in one source file and

GOD['meow']["woof"]()

in another.

I know javascript deeply enough to realize that there is no difference whatsoever. Or is there? git blame shows me that both sources were written by the same person, so it has nothing to do with style. On the one hand, what can you expect from a person, who don't hesitate to create god objects, on the other hand maybe he was in a hustle and eventually didn't have time to repay this technical debt, we'll never know.

Is it possible that using the latter method of access is safer in any way? Your opinions are welcome, fellows, before I launch my inner refactoring ninja.

shal
  • 2,854
  • 4
  • 20
  • 31
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties – Rayon Apr 11 '16 at 07:08
  • I know it all, it's basics of js, there is even more detailed description of it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors . The question is: is it all, is there anything else in it? Something deeper? – shal Apr 11 '16 at 07:09
  • 1
    A frequent reason: there's a minifier (for example google closure) which needs the second form to know it must preserve the "meow" and "woof" names. – Denys Séguret Apr 11 '16 at 07:12
  • 1
    Using bracket notation you can have dynamic programatically generated property name, and you can have special characters as well in property name, which is not possible with . notation. – Avinash Apr 11 '16 at 07:12
  • 1
    @shal, If you _do_, This question does not make any sense.. _there is no difference whatsoever_ ? I will strongly disagree... Provided link answers all the questions.. Also read [this](http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) and [this](http://stackoverflow.com/questions/17189642/difference-between-using-bracket-and-dot-notation) – Rayon Apr 11 '16 at 07:13
  • @RayonDabre Both links and most comments here are no help in the specific case given by OP – Denys Séguret Apr 11 '16 at 07:16
  • 1
    @DenysSéguret your answer seems to be it! Those parts, where it uses bracket accessor - do actually pass through minifier! Could you please add it not as a comment, but as an answer to the question? Thank you! – shal Apr 11 '16 at 07:16

1 Answers1

2

A frequent reason: there's a minifier which needs the second form to know it must preserve the "meow" and "woof" names. That's especially convenient with Google Closure.

Other than that, there's no reason, as you already saw in the documentations (using special characters and dynamic strings don't apply in your case).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758