javascript 带有“var”和不带“var”的javascript全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6901801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:17:10  来源:igfitidea点击:

javascript global variable with 'var' and without 'var'

javascriptglobalvar

提问by Moon

Possible Duplicate:
Difference between using var and not using var in JavaScript

可能的重复:
在 JavaScript 中使用 var 和不使用 var 的区别

I understand that I should always use 'var' to define a local variable in a function.

我知道我应该始终使用“var”来定义函数中的局部变量。

When I define a global function, what's the difference between using 'var' ?

当我定义一个全局函数时,使用 'var' 有什么区别?

Some of code examples I see over the internet use

我在互联网上看到的一些代码示例使用

var globalVar = something;
globalVar = something;

What's the difference?

有什么不同?

回答by CMS

Well, the difference is that technically, a simple assignment as globalVar = 'something';doesn't declare a variable, that assignment will just create a propertyon the global object, and the fact that the global object is the last object in the scope chain, makes it resolvable.

好吧,区别在于技术上,一个简单的赋值globalVar = 'something';没有声明一个变量,该赋值只会在全局对象上创建一个属性,并且全局对象是作用域链中的最后一个对象这一事实使其可解析.

Another difference is the way the binding is made, the variables are bound as "non-deletable" properties of its environment record, for example:

另一个区别是绑定的方式,变量被绑定为其环境记录的“不可删除”属性,例如:

var global1 = 'foo';
delete this.global1; // false

global2 = 'bar';
delete this.global2; // true

I would strongly encourage you to always use the varstatement, your code for example will break under ECMAScript 5 Strict Mode, assignments to undeclared identifiers are disallowed to avoid implicit globals.

我强烈建议您始终使用该var语句,例如您的代码将在 ECMAScript 5 严格模式下中断,不允许对未声明的标识符进行赋值以避免隐式全局变量

回答by jAndy

Short: There is no difference in the global context**.

简短:在全局上下文中没有区别**。

Long: The Variable objectfor the global contextis the global context itself. That is why we can just access global variablesand methodswithin a function-, or eval context. However, the varstatement just makes sure you're defining a variable in the current context, so it makes no difference by omitting that in the global context. Exception: ES5 strict mode will probably throw an error when it sees a declaration without var.

长:该变量对象全球范围内为全球范围内的本身。这就是为什么我们只能访问 globalvariablesmethodsfunction-eval 上下文中。但是,该var语句只是确保您在当前上下文中定义了一个变量,因此在全局上下文中省略它没有任何区别。例外:ES5 严格模式在看到没有var.



** the only difference is, that varwill declare a variable without definition in the current (execution) context. This happens at js parse time, so the engine knows that there is a variable with that name available in the context. Omitting varwill end up in a direct property access from the global object.

** 唯一的区别是,var它将在当前(执行)上下文中声明一个没有定义的变量。这发生在 js 解析时,因此引擎知道上下文中存在具有该名称的变量。省略var将导致从global object.