在多个for循环中使用相同的变量名是不好的做法吗? [英] Is it bad practice to use the same variable name in multiple for-loops?

查看:956
本文介绍了在多个for循环中使用相同的变量名是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用JSHint编写了一些JavaScript代码。在代码中,我有两个for循环都使用这样的:

  for(var i = 0; i< somevalue; i ++){...} 

所以for循环都使用var i来进行迭代。现在,JSHint向我展示了第二个for循环的错误:'i'已经定义好了。我不能说这是不正确的(因为它显然是),但我一直认为这并不重要,因为我只用在那个特定的地方。

使用for循环这种方法是不好的做法吗?我应该在代码中为每个for循环使用一个不同的变量,比如

  // for-loop 1 
for (var i = 0; ...; i ++){...}

// for-loop 2
for(var j = 0; ...; j ++){或者是我可以忽略的错误(因为它不会中断)我的代码,它仍然做它应该做的)?

JSLint btw。因为我没有在函数顶部定义var i(这就是为什么我首先切换到JSHint),所以停止在第一个for循环验证。所以根据这个问题的例子:我应该使用吗? JSLint或JSHint JavaScript验证? - 我应该使用这样的循环来确认JSLint:

  .. 。
var i;
...
// for-loop 1
for(i = 0; ...; i ++){...}
...
/ / for-loop 2
for(i = 0; ...; i ++){...}

这对我来说也很好,因为这样我就可以避免JSLint和JSHint中的这两个错误。但是我不确定的是,如果我应该为每个for循环使用一个不同的变量,像这样:

$ $ p $ $ $ $ $ $
var i,j;
...
// for-loop 1
for(i = 0; ...; i ++){...}
// for-loop 2
for(j = 0; ...; j ++){...}

有一个最好的做法,或者我可以只是去与上面的任何代码,这意味着我选择我的最佳做法? 解决方案

由于变量声明被提升到它们出现的范围的顶部,所以解释器将以相同的方式有效地解释这两个版本。因此,JSHint和JSLint建议将声明移出循环初始化程序。



以下代码...

(var i = 5; i <15; i ++){} $ b为(var i = 0; i <10; i ++){}

  $ b  

...被有效地解释为:

  var i; (i = 5; i <15; i ++){} 
<
为(i = 0; i <10; i ++) / pre>

请注意,实际上只有一个 i 的声明和多个赋值 - t实际上重新声明一个变量在同一范围内。

实际上回答你的问题...


是否有最佳做法,或者我可以只用上面的任何代码?

对如何最好地处理这个问题有不同的意见。就我个人而言,我同意JSLint,并认为当你在每个范围的顶部声明所有的变量时,代码更清晰。因为这就是代码的解释方式,所以为什么不写代码呢?



但是,正如你所看到的,代码无论如何都可以工作采取这样的风格/公约的选择,你可以使用任何你觉得最舒服的形式。


I was just linting some JavaScript code using JSHint. In the code I have two for-loops both used like this:

for (var i = 0; i < somevalue; i++) { ... }

So both for-loops use the var i for iteration.

Now JSHint shows me an error for the second for-loop: "'i' is already defined". I can't say that this isn't true (because it obviously is) but I always thought this wouldn't matter as the var i is only used in that specific place.

Is it bad practice to use for-loops this way? Should I use a different variable for each for-loop in my code like

//for-loop 1
for (var i = 0; ...; i++) { ... }

//for-loop 2
for (var j = 0; ...; j++) { ... }

Or is this on e of the errors I can ignore (because it doesn't break my code, it still does what it is supposed to do)?

JSLint btw. stops validating at the first for loop because I don't define var i at the top of the function (that's why I switched to JSHint in the first place). So according to the example in this question: Should I use JSLint or JSHint JavaScript validation? – I should use for-loops like this anyway to confirm JSLint:

...
var i;
...
//for-loop 1
for (i = 0; ...; i++) { ... }
...
//for-loop 2
for (i = 0; ...; i++) { ... }

This also looks good to me, because this way I should avoid both errors in JSLint and JSHint. But what I am uncertain about is if I should use a different variable for each for-loop like this:

...
var i, j;
...
//for-loop 1
for (i = 0; ...; i++) { ... }
//for-loop 2
for (j = 0; ...; j++) { ... }

So is there a best practice for this or could I just go with any of the codes above, meaning I choose "my" best practice?

解决方案

Since variable declarations are hoisted to the top of the scope in which they appear the interpreter will effectively interpret both versions in the same way. For that reason, JSHint and JSLint suggest moving the declarations out of the loop initialiser.

The following code...

for (var i = 0; i < 10; i++) {}
for (var i = 5; i < 15; i++) {}

... is effectively interpreted as this:

var i;
for (i = 0; i < 10; i++) {}
for (i = 5; i < 15; i++) {}

Notice that there is really only one declaration of i, and multiple assignments to it - you can't really "redeclare" a variable in the same scope.

To actually answer your question...

is there a best practice for this or could I just go with any of the codes above?

There are varying opinions on how best to handle this. Personally, I agree with JSLint and think the code is clearer when you declare all variables together at the top of each scope. Since that's how the code will be interpreted, why not write code that looks as it behaves?

But, as you've observed, the code will work regardless of the approach taken so it's a style/convention choice, and you can use whichever form you feel most comfortable with.

这篇关于在多个for循环中使用相同的变量名是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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