简体   繁体   English

JavaScript全局变量声明语法

[英]JavaScript global variable declaration syntax

I'm writing a Javascript library that I hope to be able to minify with the Closure Compiler's ADVANCED_OPTIMIZATIONS option. 我正在编写一个Javascript库,希望能够使用Closure Compiler的ADVANCED_OPTIMIZATIONS选项进行缩小。 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? 我的问题是,使用这种语法是否有任何缺点,并且自IE 6以来发布的所有浏览器都支持它吗? (Or should I be using a completely different technique altogether?) (或者我应该完全使用完全不同的技术?)

Although 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 . 虽然两者都是全局对象的属性,但存在差异:当您使用var声明变量时,其[[Configurable]]内部属性将设置为false Therefore, it's not possible to change its attributes with Object.defineProperty (except for [[Value]] ). 因此,无法使用Object.defineProperty更改其属性( [[Value]]除外)。 The most notable effect is that such variables cannot be delete d: 最显着的效果是这些变量无法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. 此外,在为属性分配变量时,可以对该值进行COPY而不是引用该变量。 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 welcome! 别客气!

It will work; 它会起作用; it's perfectly valid syntax; 它是完全有效的语法; and it's supported in IE6 and up. 它在IE6及以上版本中得到支持。

Demo: http://ie6test.it/?url=http://jsbin.com/usafeg/2 演示: http//ie6test.it/?url = http://jsbin.com/usafeg/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM