我什么时候使用var?

发布于 2024-11-14 15:44:55 字数 273 浏览 1 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

叹沉浮 2024-11-21 15:44:55

首先,在函数之外使用代码通常是不好的做法。如果不出意外的话,将代码包装在匿名函数中:

(function(){
    // code
})();

至于 var 有什么作用,它“声明”了一个变量:

var foo;
alert(foo); // undefined;

vs:

alert(foo); // error: foo is not defined

这样做的原因是上面的代码在功能上与:

alert(window.foo);

不使用 var 声明变量,您会收到查找错误,与尝试访问任何不存在的对象的属性相同。

请注意,var 的奇怪之处之一是所有声明都被拉到脚本的顶部,因此这也将起作用:

alert(foo); // undefined
var foo;

您还可以在 window< /code> 对象(尽管您也可以通过设置不带 var 的变量来获得此对象,例如仅 foo=42):

var foo;
for(var key in window){
   // one of these keys will be 'foo'
}

First of all, it's generally bad practice to use code outside of functions. If nothing else, wrap your code in anonymous functions:

(function(){
    // code
})();

As for what effect var has, it "declares" a variable:

var foo;
alert(foo); // undefined;

vs:

alert(foo); // error: foo is not defined

The reason for this is that the above code is functionally identical to:

alert(window.foo);

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:

alert(foo); // undefined
var foo;

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. just foo=42):

var foo;
for(var key in window){
   // one of these keys will be 'foo'
}
脱离于你 2024-11-21 15:44:55

始终使用var是一种很好的做法。严格来说,当您已经处于全局范围内时,您不需要这样做,但为了代码的可维护性,您应该这样做。

假设您有:

foo = 'bar';

但后来您决定将此代码移至函数中:

function doSomething() {
    foo = 'bar'; // oops forgot to add var 
}

如果您忘记添加 var 语句,则您刚刚创建了一个隐式全局变量。现在,如果您在全局范围内创建一个名为 foo 的新对象,它们将导致彼此冲突。

function doSomething() {
    foo = 'bar'; // Implicit global
}

foo = 'baz';
doSomething();
console.log(foo); // Returns 'bar', not 'baz'

当您忘记在 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:

foo = 'bar';

But later you decide you want to move this code into a function:

function doSomething() {
    foo = 'bar'; // oops forgot to add var 
}

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 named foo they will cause conflicts with one another.

function doSomething() {
    foo = 'bar'; // Implicit global
}

foo = 'baz';
doSomething();
console.log(foo); // Returns 'bar', not 'baz'

This kind of error is particularly insidious when you forget to use var on something like i in a for loop. Learning to use JSLint can help to avoid these and other problematic logic or syntax errors.

抹茶夏天i‖ 2024-11-21 15:44:55

您的问题已在 https://developer.mozilla 中得到解答。 org/en-US/docs/Web/JavaScript/Reference/Statements/var

在函数外部使用 var 是
选修的;给一个赋值
隐式未声明的变量
将其声明为全局变量。
但是,建议始终
使用 var,并且必须在
在以下情况下起作用:

  • 如果包含该函数的作用域(包括全局作用域)中的变量具有相同的名称。
  • 如果递归或多个函数使用同名变量并且>希望这些变量是本地的。

声明变量失败
这些案例很可能会导致
意想不到的结果。

Your question is answered in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Using var outside a function is
optional; assigning a value to an
undeclared variable implicitly
declares it as a global variable.
However, it is recommended to always
use var, and it is necessary within
functions in the following situations:

  • If a variable in a scope containing the function (including the global scope) has the same name.
  • If recursive or multiple functions use variables with the same name and> intend those variables to be local.

Failure to declare the variable in
these cases will very likely lead to
unexpected results.

初懵 2024-11-21 15:44:55

我相信在函数之外使用 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

清风不识月 2024-11-21 15:44:55

我相信每当你初始化一个变量时你都想创建一个 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.

殊姿 2024-11-21 15:44:55

如果您声明一个全局变量并设置一个值,它将没有任何实际价值,但如上所述,这是最佳实践。但是,如果您想声明一个没有值的变量,则需要“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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文