I'm writing a Javascript library that I hope to be able to minify with the Closure Compiler's ADVANCED_OPTIMIZATIONS option. The library has maybe two dozen global variables which set lower and upper range limits, string literals, etc.
To make these variable accessible from other source files and to avoid dead code removal, I have to "export" them. See Advanced Compilation and Externs.
Therefore, rather than declare variables with this syntax:
var fooMinValue = 10;
I plan to use this syntax:
window['fooMinValue'] = 10;
I've tested this and it seems to work fine. My questions are, is there any downside to using this syntax and is it supported in all browsers released since IE 6? (Or should I be using a completely different technique altogether?)
thisto access the global object. Also if your names are alphanumerics, you don't need the bracket-notation:this.fooMinValue = 10;.this.fooMinValue = 10;JSC_USED_GLOBAL_THIS: dangerous use of the global this object at line 30 character 0 this.fooMinValue = 10;. Regardless, I can't usethisin this case because since the varible is not enclosed in quotes, Closure renames it. Useful (for me) comment, none the less.this... Consider this pattern:(function ( root ){ root['fooMinValue'] = 10; }( this ));So you capture the global object into therootparameter, and then use that parameter to set the global properties. This way, you can still be agnostic about accessing the global object (you don't depend onwindow).