admin管理员组文章数量:1428481
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 pletely different technique altogether?)
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 pletely different technique altogether?)
Share Improve this question edited Oct 26, 2012 at 17:29 bfavaretto 71.9k18 gold badges117 silver badges159 bronze badges asked Oct 26, 2012 at 17:23 KarlKarl 1,8641 gold badge25 silver badges40 bronze badges 4-
1
In global code, you can use
this
to access the global object. Also if your names are alphanumerics, you don't need the bracket-notation:this.fooMinValue = 10;
. – Šime Vidas Commented Oct 26, 2012 at 17:35 -
@ŠimeVidas FYI: Closure generates the following warning for this syntax
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 usethis
in this case because since the varible is not enclosed in quotes, Closure renames it. Useful (for me) ment, none the less. – Karl Commented Oct 26, 2012 at 17:53 -
Hm, I'm not sure what's so dangerous about using the global
this
... Consider this pattern:(function ( root ){ root['fooMinValue'] = 10; }( this ));
So you capture the global object into theroot
parameter, 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
). – Šime Vidas Commented Oct 26, 2012 at 17:59 - @ŠimeVidas I was checking out the piler warning and came across this Q Closure-piler-warning-dangerous-use-of-the-global-this-object. – Karl Commented Oct 26, 2012 at 23:40
3 Answers
Reset to default 2Although both are properties of the global object, there is a difference: when you declare the variable with var
, its [[Configurable]]
internal attribute gets set to false
. Therefore, it's not possible to change its attributes with Object.defineProperty
(except for [[Value]]
). The most notable effect is that such variables cannot be delete
d:
var foo = 'bar';
window['bar'] = 'baz';
console.log(foo); // 'bar'
console.log(bar); // 'baz'
delete foo; // doesn't work, you can't delete vars
delete bar; // works, bar is an object property
console.log(foo); // 'bar'
console.log(bar); // ReferenceError
Also, when assigning a variable to a property, you make a COPY of the value instead of referencing the variable. This means external changes to the property don't affect the value of the variable.
(function() {
var foo = 'bar';
window['foo2'] = foo; //export foo
console.log(foo); // will output 'bar'
setTimeout(function() { console.log(foo) }, 1000); //will output 'bar'
})();
window['foo2'] = 'baz';
console.log(window['foo2']); // will output 'baz'
The above code will produce the following output:
'bar'
'baz'
'bar'
It is the same except that if your script is not running on a browser it is very probable that window will be undefined.
You are wele!
It will work; it's perfectly valid syntax; and it's supported in IE6 and up.
Demo: http://ie6test.it/?url=http://jsbin./usafeg/2
本文标签: JavaScript global variable declaration syntaxStack Overflow
版权声明:本文标题:JavaScript global variable declaration syntax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745466913a2659570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论