var关键字真的有必要吗? [英] is the var keyword really necessary?

查看:90
本文介绍了var关键字真的有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否建议在声明变量时使用var关键字?


IE允许我使用var创建变量,然后再次创建同一个

没有问题。

它还允许我创建一个没有var关键字的变量。因此下面的

代码工作正常:


< html>

< script>

test = 1;

var test2 = 2;

var test = 3;

var test2 = 4;

提醒(测试);

提醒(test2);

< / script>

< / html>


另外,是否有充分的理由不对以下的陈述做:


for(li = 0; li< 10; li ++){;}


而不是:


for(var li = 0; li< 10; li ++){;}

干杯,

解决方案

2004年10月4日07:39:22 -0700,Jack写道:

在声明变量时是否建议使用var关键字?



yes,''因为你用var关键字定义变量的范围。


例如:


var i = 12; / *我是全球性的* /


函数foo(){

var j = 2; / * j是本地的* /

提醒(i + j);

}


alert(i);

foo();

alert(j); / *错误! j是本地的* /


尝试使用相同的代码而不用var关键字:


i = 12; / *我是全球性的* /


函数foo(){

j = 2; / * j也是全球性的* /

alert(i + j);

}


alert(i);

foo();

alert(j);


如你所见,没有var关键字,所有变量都是全局的,并且你可以

将一些变量从函数覆盖到另一个函数。


另外,使用var你声明变量的关键字,即使你没有给b $ b分配一些价值:


var x;


alert(窗口中的x); // true

alert(窗口中的y); // false


希望它有所帮助。


PS

对不起我的英语。


-

ZER0://coder.gfxer.web-designer /


~Massi''che e' 'uno standard,e''uno standard proprietario。"

(Gio'')


播出〜Motion City Soundtrack - 我最喜欢的事故 ;


Jack< jc ******** @ gmail.com>这样说:

此外,有充分的理由不对以下语句做:
for(li = 0; li< 10; li ++){;}
而不是:
for(var li = 0; li< 10; li ++){;}




第二个是误导性的,因为li有函数范围,而不是

,因为写完后他们很难读。


for(li = 0; li< 10; li ++){ ;}


更容易恕我直言。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


2004年10月4日07:39:22 -0700,杰克< jc ******** @ gmail.com>写道:

是否建议在声明变量时使用var关键字?


对于全局变量,不,但它被认为是好形式。从

开始,var关键字清楚地表明该语句是一个变量

声明(可能是初始化)。

IE让我创建一个变量与var,然后再次创建相同的
没有问题。


我不认为这是语法错误,但没有太多意义。

它还可以让我创建一个变量而不用var关键字。因此下面的
代码工作正常:

< html>
< script>
test = 1;
var test2 = 2;
var test = 3;
var test2 = 4;
alert(test);
alert(test2);
< / script>
< / HTML>


没关系,因为所有变量都是全局的。


myVar = 1;





var myVar = 1;


在该范围级别相同。 var关键字变得非常不同

函数内部。

另外,是否有充分的理由不对以下语句做什么:

for(li = 0; li< 10; li ++){;}

代替:

for(var li = 0; li< 10; li ++){;}



同样,在全球范围内,没有任何问题。但是,请考虑这个[1]:


函数myFunction(){

for(i = 0; i< 5; ++ i){

//做点什么

myFunction();

}

}


当递归调用函数时,变量i每次都重置为零

。当早期的调用最终重新获得控制权时,我将

总是为5。这不太可能是预期的行动。


养成总是使用var关键字的习惯对于

来说是一件好事。我的看法。它避免了如果你忘记了
添加一次就可能出现的问题。同样,变量应该*始终*尽可能地限制范围

。有很多方法可以避免在任何地方放置全局变量
,而不仅限于使用对象和闭包。它不仅可以减少错误的机会,而且还可以使代码更加可重复使用,而不是因为污染全局命名空间而导致代码更加可重用。


Mike

[1]暂时忽略无限递归。这个例子对于

来说并不重要。


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。


Hi,

Is it recommended to use the var keyword when declaring a variable?

IE lets me create a variable with var, then create the same one again
with no problem.
Also it lets me create a variable without the var keyword. As such the
code below works fine:

<html>
<script>
test = 1;
var test2 = 2;
var test = 3;
var test2 = 4;
alert(test);
alert(test2);
</script>
</html>

Also, is there a good reason not to do for statements like:

for (li=0;li<10;li++){;}

instead of:

for (var li=0;li<10;li++){;}
Cheers,

解决方案

On 4 Oct 2004 07:39:22 -0700, Jack wrote:

Is it recommended to use the var keyword when declaring a variable?



yes, ''cause you define the scope of your variables with var keyword.

for example:

var i=12; /* i is global */

function foo(){
var j=2; /* j is local */
alert(i+j);
}

alert(i);
foo();
alert(j); /* error! j is local */

try the same code without var keyword:

i=12; /* i is global */

function foo(){
j=2; /* j is global as well */
alert(i+j);
}

alert(i);
foo();
alert(j);

As you see, without the var keyword all variables are global, and you could
do overwrite some variable from function to another function.

In addition, with "var" keyword you declare the variable, even if you don''t
assign it some value:

var x;

alert("x" in window); //true
alert("y" in window); //false

Hope it helps.

P.S.
Sorry for my english.

--
ZER0://coder.gfxer.web-designer/

~ "Massi'' che e'' uno standard, e'' uno standard proprietario."
(Gio'')

on air ~ "Motion City Soundtrack - My Favorite Accident"


Jack <jc********@gmail.com> spoke thus:

Also, is there a good reason not to do for statements like: for (li=0;li<10;li++){;} instead of: for (var li=0;li<10;li++){;}



This second one is misleading, because li has function scope, not
block scope (as the code seems to suggest). I personally choose
neither, because as written they''re hard to read.

for( li=0; li < 10; li++ ) {;}

Much easier IMHO.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


On 4 Oct 2004 07:39:22 -0700, Jack <jc********@gmail.com> wrote:

Is it recommended to use the var keyword when declaring a variable?
For global variables, no, but it is considered good form. Starting with
the var keyword makes it clear that the statement is a variable
declaration (and possibly initialisation).
IE lets me create a variable with var, then create the same one again
with no problem.
I don''t think it''s a syntax error, but there isn''t much point.
Also it lets me create a variable without the var keyword. As such the
code below works fine:

<html>
<script>
test = 1;
var test2 = 2;
var test = 3;
var test2 = 4;
alert(test);
alert(test2);
</script>
</html>
It is fine, because all of the variables are global.

myVar = 1;

and

var myVar = 1;

are identical at that scope level. The var keyword becomes very different
within functions.
Also, is there a good reason not to do for statements like:

for (li=0;li<10;li++){;}

instead of:

for (var li=0;li<10;li++){;}



Again, in global scope, there isn''t any problem. However, consider this[1]:

function myFunction() {
for(i = 0; i < 5; ++i) {
// do something
myFunction();
}
}

When the function is called recursively, the variable, i, is reset to zero
every time. When earlier invocations eventually regain control, i will
always be five. That isn''t likely to be the intended action.

Getting into the habit of always using the var keyword is a good thing to
do, in my opinion. It avoids problems that could arise if you forget to
add it one time. Similarly, variables should *always* be limited in scope
as much as possible. There are many ways to avoid placing globals
everywhere, not limited to using objects and closures. Not only does it
reduce the chances of errors, it also makes code more reusable by not
polluting the global namespace.

Mike
[1] Ignore the infinite recursion for the moment. That isn''t important for
this example.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


这篇关于var关键字真的有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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