全局变量和全局对象的属性之间是否有区别? [英] Is there any difference between a global variable and a property of the Global Object

查看:152
本文介绍了全局变量和全局对象的属性之间是否有区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读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 属性



参考文献:


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:

这篇关于全局变量和全局对象的属性之间是否有区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆