1

We using google closure as the build tool for a medium size javascript project. To make it easier to protect the code we turned on the ADVANCED_OPTIMIZATION option. The result were lots of dangerous use of the global this object warnings.

There are several workarounds discussed like here but I dont find them really satisfying. The first thing annoying me is to put everything in the global namespace. Isnt this a really bad architecture style from the old days of functional programming? This means changes throughout the whole project whenever someting global changes...

The other solution would be to use the @constructor annotation throughout the code. This would change nearly every object method into a (documented) constructor (we have more than thousand ...). Before starting to annotate everything: is it safe to use the annotation method (e.g. is the code working after optimization)? Maybe the code is not obfuscated within the annotated methods?

Is there really no better alternative as these two above?

Community
  • 1
  • 1
stot
  • 1,036
  • 10
  • 20
  • Are they just warnings? Will they have any effect on the operation of your program? Is eliminating the warnings a sufficient benefit to overcome the obvious drawbacks of making the code changes? – Robert Harvey Jan 11 '14 at 00:21
  • The code is not working after optimization. I dont even know the name of the main entry point for the application: new window.myApp() doesnt exist. – stot Jan 11 '14 at 00:28
  • So they're not warnings, then? They're errors. – Robert Harvey Jan 11 '14 at 00:30
  • The compiler outputs warnings: 'WARNING - dangerous use of the global this object'. But yes, according to the docs the symbols will be destroyed: 'this.color' will be mangled to just 'a' which is not working. – stot Jan 11 '14 at 00:35
  • Seems like pragmatism overrides fear of theft in this case. There's nothing to steal if you can't get it to work. – Robert Harvey Jan 11 '14 at 00:36

0 Answers0