全局变量和全局对象的属性之间是否有区别? [英] Is there any difference between a global variable and a property of the Global Object
问题描述
我正在阅读David Mark关于js框架Sencha的以下分析: https://gist.github .com / 3279190 ,并在那里他说... ...
$ b
他们想要的是一个全局变量,但他们最终与全球对象的属性。根据规范和(和实施历史),两者之间有足够的区别,需要注意不要混淆(如此处所示)。
...但据我所知, var my_global = 123;
和(在浏览器环境中)<$ c之间没有任何区别$ c> window.my_global = 123; (在这个例子中,我假定环境是一个浏览器 - 因此使用窗口
,但我可以刚刚使用 this.my_global
,相反,当在不同的环境中运行时,全局对象显然不同)。
但是忽略这个小的差异是为全局对象分配一个属性并创建一个全局变量?我认为不是,创建一个全局变量只是将属性赋值给全局对象的另一种方式。
我相信在某些浏览器中可能会有问题,如果它们有一个id为my_global的元素,显然这可能会导致JavaScript引用正确的问题,但我不知道如何/是什么原因导致该问题(例如,是否将属性分配给全局对象导致元素ID问题发生,还是它声明一个全局变量,导致元素ID问题?)
有人可以为我澄清这一点吗?
<在全局范围内使用
var
创建的变量确实创建了全局对象的一个属性。然而,这个属性的行为与全局对象的属性不同,它没有使用 var
创建。 首先,执行变量声明的方式有所不同:全局范围中的 var
语句在执行任何代码之前创建全局对象的属性,这通常是一种效果被称为 hoisting ,在网络上有很好的记录(见下面的参考文献)。其次,全局变量不像全局对象的属性使用删除
运算符不能删除 var
不能被删除的内容(虽然这是在旧版IE浏览器中不适用))。 delete
不能用于删除变量。这种差异归结为内部的属性,每个对象属性具有。这些属性在ECMAScript规范中指定。在ECMAScript 5中, var foo =bar
使用<$ c创建全局对象的属性 foo
$ c> [[Configurable]] 属性 false
,而 this.foo =bar
(在全局范围内)使用 [[Configurable]]
属性 true创建一个
foo
属性
参考文献: 德米特里·索什尼科夫在他出色的 kangax文章包含大量 安格斯克罗尔的变量与JavaScript文章中的属性,链接到许多与此答案相同的资源。 规范: ECMAScript 5.1 。
系列文章中详细写过这篇文章, ECMAScript 262- 3详细。我建议
阅读第2章的全部内容,但最相关的部分称为
关于变量。
相关信息以及浏览器错误和偏差的详细信息,
加上窗口的进一步怪癖。
I was reading the following analysis from David Mark about the js framework "Sencha": https://gist.github.com/3279190 and in there he states...
What they wanted was a global variable, but they ended up with is a property of the Global Object. According the specifications and (and implementation history) there are enough differences between the two that care is required not to mix them up (as is done here).
...but as far as I was aware there wasn't any difference between var my_global = 123;
and (in a browser environment) window.my_global = 123;
(in that example I assumed the environment was a browser - hence the use of window
, but I could have just used this.my_global
instead as obviously the Global Object would be different when run in different environments).
But ignoring that minor discrepancy is there a difference between assigning a property to the Global Object and creating a global variable? I thought not, and that creating a global variable was just another way to assign a property to the Global Object.
I believe there can be an issue in some browsers if they had an element with an id of "my_global" then apparently that can cause problems with JavaScript referencing the right thing, but I'm not sure how/what causes that problem (e.g. does assigning a property to the Global Object cause the element id issue to occur, or is it declaring a global variable that causes the element id issue?)
Can someone clarify this for me please?
A variable created using var
in the global scope does create a property of the global object. However, this property has different behaviour from a property of the global object that has not been created using var
.
Firstly, there is a difference in how a variable declaration is executed: a var
statement in the global scope creates a property of the global object before any code is executed, an effect commonly known as hoisting, well documented around the web (see references below).
Secondly, the global variable, unlike a property of the global object that has not been created with var
, cannot be deleted using the delete
operator (although this is not true in older versions of IE). delete
cannot be used to delete variables. This difference is down to internal property attributes that every object property has. These attributes are specified in the ECMAScript specification. In ECMAScript 5 terms, var foo = "bar"
creates a property foo
of the global object with the [[Configurable]]
attribute false
whereas this.foo = "bar"
(in global scope) creates a foo
property with [[Configurable]]
attribute true
.
References:
Dmitry Soshnikov has written at length about this in his excellent series of articles, ECMAScript 262-3 in detail. I recommend reading all of chapter 2, but the most relevant section is called About Variables.
The kangax article linked earlier contains a lot of relevant information and details of browser bugs and deviations, plus further quirks concerning
window
.Angus Croll's Variables vs. Properties in JavaScript article, which links to many of the same resources as this answer.
The spec: ECMAScript 5.1.
这篇关于全局变量和全局对象的属性之间是否有区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!