Possible Duplicate:
Javascript: is using 'var' to declare variables optional?可能的重复:Javascript:使用“var”声明变量是可选的吗?
When creating variables in javascript is adding "var" before the variable name a must?
在javascript中创建变量是在变量名a之前添加“var”?
For example instead of
例如,而不是
var message = "Hello World!"
can I use
我可以用
message = "Hello World!"
?
吗?
I notice that scripts like Google Adsense don't use var
我注意到像谷歌Adsense这样的脚本不使用var
Example:
例子:
google_ad_width = 160;
google_ad_height = 600;
google_color_border = "000000";
google_color_bg = "ffffff";
6 个解决方案
#1
97
Without the var
you create a global, and globals are a fantastic way to have different functions overwriting each others variables (i.e. they make code a pain to maintain).
没有了var,您就创建了一个全局变量,而globals是一种非常奇妙的方法,可以让不同的函数覆盖每个其他变量(也就是说,它们使代码难以维护)。
With the var
, the scope of the variable is limited to the current function (and anything inside it — it is possible to nest functions).
对于var,变量的范围仅限于当前函数(以及其中的任何内容——可以嵌套函数)。
Google Adsense uses globals because it splits scripts into two distinct parts (one local and one remote). A cleaner approach would be to call a function defined in the remote script and pass the parameters as arguments instead of having it pick them up from the global scope.
谷歌Adsense使用全局变量,因为它将脚本分为两个不同的部分(一个本地部分和一个远程部分)。一种更简洁的方法是调用在远程脚本中定义的函数,并将参数作为参数传递,而不是让它从全局范围中获取参数。
#2
7
Yes, you should always use var
.
是的,您应该始终使用var。
Not using var
has two major drawbacks:
不使用var有两个主要缺点:
- Accessing a variable within a function that is not defined within that function will cause the interpreter to look up the scope chain for a variable with that name until either it find one or it gets to the global object (accessible in browsers via
window
) where it will create a property. This global property is now available everywhere, potentially causing confusion and hard-to-detect bugs; - 访问一个变量在一个函数内定义的函数,它不是将导致翻译查找一个变量的作用域链这个名字,直到它找到一个或到达全局对象(在浏览器访问通过窗口),它将创建一个属性。这个全局属性现在无处不在,可能导致混乱和难以检测的bug;
- Accessing an undeclared variable will cause an error in ECMAScript 5 strict mode.
- 访问未声明的变量将导致ECMAScript 5严格模式中的错误。
Also, not using var
for global variable is not exactly the same as using var
: when using var
, the property it creates on the global object has the internal DontDelete
attribute, which is not the case without var
:
此外,不将var用于全局变量并不完全等同于使用var:当使用var时,它在全局对象上创建的属性具有内部的DontDelete属性,没有var就不是这样:
// Next line works in any ECMAScript environment. In browsers, you can
// just use the window object.
var globalObj = (function() { return this; })();
var x = 1;
delete globalObj.x;
alert(x); // Alerts 1, x could not be deleted
y = 2;
delete globalObj.y;
alert(y); // Error, y is undefined
#3
4
From http://www.updrift.com/article/to-var-or-not-to-var-my-javascript
从http://www.updrift.com/article/to-var-or-not-to-var-my-javascript
- For global variables, it doesn’t matter, but you may want to use it for consistency.
- 对于全局变量,这并不重要,但是您可能希望使用它来保持一致性。
- Always try to use ‘var’ to declare variables in local functions. It makes sure you’re using a local copy of the variable instead of another variable of the same name in a different scope.
- 总是尝试使用' var '来声明本地函数中的变量。它确保您正在使用变量的本地副本,而不是在不同范围中使用同名的另一个变量。
For example, the two similar functions here have very different effects:
例如,这里两个相似的函数有非常不同的效果:
var myvar = 0;
function affectsGlobalVar(i){
myvar = i;
}
function doesNotAffectGlobalVar(i){
var myvar = i;
}
#4
3
Without var a variable is defined as global.
没有var,变量定义为全局变量。
#5
1
When adding Google AdSense to your page, you're linking a number of javascript files. The variables are defined in there.
在向页面添加谷歌AdSense时,您正在链接许多javascript文件。变量是在这里定义的。
http://pagead2.googlesyndication.com/pagead/show_ads.js should be one of the script references in your page.
http://pagead2.googlesyndication.com/pagead/show_ads.js应该是页面中的脚本引用之一。
#6
1
For the unenlightened like myself, JavaScript basically has two scopes for variables: global and local. Variables assigned outside of a function are global, and variables assigned inside of a function, using the var keyword, are local (not rocket surgery). However, if you leave the var keyword off, it assigns a global variable, regardless of where it’s declared.
对于像我这样的未开化的人,JavaScript基本上有两个变量作用域:全局变量和局部变量。函数外部分配的变量是全局的,函数内部分配的变量使用var关键字是局部的(不是rocket surgery)。但是,如果您不使用var关键字,它会分配一个全局变量,不管它声明在哪里。
From: http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript
来自:http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript
#1
97
Without the var
you create a global, and globals are a fantastic way to have different functions overwriting each others variables (i.e. they make code a pain to maintain).
没有了var,您就创建了一个全局变量,而globals是一种非常奇妙的方法,可以让不同的函数覆盖每个其他变量(也就是说,它们使代码难以维护)。
With the var
, the scope of the variable is limited to the current function (and anything inside it — it is possible to nest functions).
对于var,变量的范围仅限于当前函数(以及其中的任何内容——可以嵌套函数)。
Google Adsense uses globals because it splits scripts into two distinct parts (one local and one remote). A cleaner approach would be to call a function defined in the remote script and pass the parameters as arguments instead of having it pick them up from the global scope.
谷歌Adsense使用全局变量,因为它将脚本分为两个不同的部分(一个本地部分和一个远程部分)。一种更简洁的方法是调用在远程脚本中定义的函数,并将参数作为参数传递,而不是让它从全局范围中获取参数。
#2
7
Yes, you should always use var
.
是的,您应该始终使用var。
Not using var
has two major drawbacks:
不使用var有两个主要缺点:
- Accessing a variable within a function that is not defined within that function will cause the interpreter to look up the scope chain for a variable with that name until either it find one or it gets to the global object (accessible in browsers via
window
) where it will create a property. This global property is now available everywhere, potentially causing confusion and hard-to-detect bugs; - 访问一个变量在一个函数内定义的函数,它不是将导致翻译查找一个变量的作用域链这个名字,直到它找到一个或到达全局对象(在浏览器访问通过窗口),它将创建一个属性。这个全局属性现在无处不在,可能导致混乱和难以检测的bug;
- Accessing an undeclared variable will cause an error in ECMAScript 5 strict mode.
- 访问未声明的变量将导致ECMAScript 5严格模式中的错误。
Also, not using var
for global variable is not exactly the same as using var
: when using var
, the property it creates on the global object has the internal DontDelete
attribute, which is not the case without var
:
此外,不将var用于全局变量并不完全等同于使用var:当使用var时,它在全局对象上创建的属性具有内部的DontDelete属性,没有var就不是这样:
// Next line works in any ECMAScript environment. In browsers, you can
// just use the window object.
var globalObj = (function() { return this; })();
var x = 1;
delete globalObj.x;
alert(x); // Alerts 1, x could not be deleted
y = 2;
delete globalObj.y;
alert(y); // Error, y is undefined
#3
4
From http://www.updrift.com/article/to-var-or-not-to-var-my-javascript
从http://www.updrift.com/article/to-var-or-not-to-var-my-javascript
- For global variables, it doesn’t matter, but you may want to use it for consistency.
- 对于全局变量,这并不重要,但是您可能希望使用它来保持一致性。
- Always try to use ‘var’ to declare variables in local functions. It makes sure you’re using a local copy of the variable instead of another variable of the same name in a different scope.
- 总是尝试使用' var '来声明本地函数中的变量。它确保您正在使用变量的本地副本,而不是在不同范围中使用同名的另一个变量。
For example, the two similar functions here have very different effects:
例如,这里两个相似的函数有非常不同的效果:
var myvar = 0;
function affectsGlobalVar(i){
myvar = i;
}
function doesNotAffectGlobalVar(i){
var myvar = i;
}
#4
3
Without var a variable is defined as global.
没有var,变量定义为全局变量。
#5
1
When adding Google AdSense to your page, you're linking a number of javascript files. The variables are defined in there.
在向页面添加谷歌AdSense时,您正在链接许多javascript文件。变量是在这里定义的。
http://pagead2.googlesyndication.com/pagead/show_ads.js should be one of the script references in your page.
http://pagead2.googlesyndication.com/pagead/show_ads.js应该是页面中的脚本引用之一。
#6
1
For the unenlightened like myself, JavaScript basically has two scopes for variables: global and local. Variables assigned outside of a function are global, and variables assigned inside of a function, using the var keyword, are local (not rocket surgery). However, if you leave the var keyword off, it assigns a global variable, regardless of where it’s declared.
对于像我这样的未开化的人,JavaScript基本上有两个变量作用域:全局变量和局部变量。函数外部分配的变量是全局的,函数内部分配的变量使用var关键字是局部的(不是rocket surgery)。但是,如果您不使用var关键字,它会分配一个全局变量,不管它声明在哪里。
From: http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript
来自:http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript