我什么时候使用var?
可能的重复:
JavaScript 变量作用域
我的理解是,在函数中如果我使用 var 那么我就有一个局部变量。如果我不 delcare var 我现在有一个全局变量。
但是函数的 oustide 呢,var 有什么作用呢?
Possible Duplicate:
JavaScript Variable Scope
My understanding it that with in a function if I use var then I have a local variable. If I do not delcare var I now have a global variable.
But what about oustide of functions, what effect does var have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,在函数之外使用代码通常是不好的做法。如果不出意外的话,将代码包装在匿名函数中:
至于 var 有什么作用,它“声明”了一个变量:
vs:
这样做的原因是上面的代码在功能上与:
不使用
var 声明变量
,您会收到查找错误,与尝试访问任何不存在的对象的属性相同。请注意,
var
的奇怪之处之一是所有声明都被拉到脚本的顶部,因此这也将起作用:您还可以在
window< /code> 对象(尽管您也可以通过设置不带 var 的变量来获得此对象,例如仅
foo=42
):First of all, it's generally bad practice to use code outside of functions. If nothing else, wrap your code in anonymous functions:
As for what effect var has, it "declares" a variable:
vs:
The reason for this is that the above code is functionally identical to:
without declaring the variable with
var
, you get a lookup error, the same as trying to access the property of any object that doesn't exist.Note that one of the oddities of
var
is that all declarations are pulled to the top of the script, so this will work as well:You will also have access to your variable in the
window
object (though you will also have this by setting the variable without var, e.g. justfoo=42
):始终使用
var
是一种很好的做法。严格来说,当您已经处于全局范围内时,您不需要这样做,但为了代码的可维护性,您应该这样做。假设您有:
但后来您决定将此代码移至函数中:
如果您忘记添加
var
语句,则您刚刚创建了一个隐式全局变量。现在,如果您在全局范围内创建一个名为foo
的新对象,它们将导致彼此冲突。当您忘记在
for
循环中的i
上使用var
时,这种错误尤其隐蔽。学习使用 JSLint 可以帮助避免这些和其他有问题的逻辑或语法错误。It is good practice to always use
var
. Strictly speaking when you are already in the global scope you don't need to but for the sake of code maintainability you should.Say you have:
But later you decide you want to move this code into a function:
If you forget to add a
var
statement you've just created an implicit global. Now if you create a new object in the global scope that is namedfoo
they will cause conflicts with one another.This kind of error is particularly insidious when you forget to use
var
on something likei
in afor
loop. Learning to use JSLint can help to avoid these and other problematic logic or syntax errors.您的问题已在 https://developer.mozilla 中得到解答。 org/en-US/docs/Web/JavaScript/Reference/Statements/var
Your question is answered in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
我相信在函数之外使用 var 与不使用 var 的效果相同:你会得到一个全局变量。例外情况是,如果您位于类或其他命名空间结构中,它仍然会在该语言环境中定义变量
I believe using var outside of a function works the same as not using var: you get a global. The exception to this would be if you are in a class or some other namespace structure, in which it will still define a variable in that locale
我相信每当你初始化一个变量时你都想创建一个 var 。正如我编码的那样,每当我需要初始化变量时,我都会用 var 启动它。如果声明一个变量时不使用 var 一词,则它始终是全局变量。如果在函数内使用 var 声明变量,则该变量是该函数的本地变量。如果在函数外部使用 var 创建变量,它将是全局变量。
i believe you want to create a var whenever you are initializing a variable. As i've coded, when ever i need to initialize a variable, i start it with var. If you declare a variable without the word var, it's always global. If you declare a variable with var, inside a function, it's local to that function. If you create a variable with var outside functions, it will be a global variable.
如果您声明一个全局变量并设置一个值,它将没有任何实际价值,但如上所述,这是最佳实践。但是,如果您想声明一个没有值的变量,则需要“var”。
If you are declaring a global variable and set a value it won't have any practical value, but as mentioned, it's best practice. If however you want to declare a variable without a value, you will need "var".