处理循环变量的重复声明警告 [英] Dealing with duplicate declaration warning for loop variables

查看:158
本文介绍了处理循环变量的重复声明警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

for (var i=0; i<100; i++) {
    // your code here
}
// some other code here
for (var i=0; i<500; i++) {
    // custom code here
}

任何体面的lint工具(jslint,jshint或内置在IDE中)都会告诉警告 - 变量i的重复声明。这可以通过使用带有其他名称的变量( k j )或将声明移到顶部来解决:

Any decent lint tool (jslint, jshint or built in IDE) will tell warning - duplicate declaration of variable i. This can be solved by using variable with another name (k, j) or moving declaration to the top:

var i; // iterator
for (i=0; i<100; i++) {}
for (i=0; i<500; i++) {}

我不喜欢这两种变体 - 我通常不会在顶部做出声明(即使我做了,我也不想看到那里的帮手变量 - i j k )真的没什么在这些例子中,为了更改变量的名称,会发生错误。

I am not fond of both variants - I don't make declarations at the top usually (and even if I did I wouldn't want see there helper variables - i, j, k) and really nothing bad is going on in those examples to change variables' names for.

虽然我确实需要一个巨大的警告,以防我写下这样的内容:

Though I do want a huge warning in case I write something like this:

for (var i=0; i<100; i++) {
    for (var i=0; i<500; i++) {} // now that's bad
}

你对这种情况有什么看法?

What's your approach to such cases?

推荐答案

JavaScript有许多结构,看起来像其他计算机语言中的众所周知的结构。像大多数其他计算机语言一样,以另一种方式解释构造是危险的。

JavaScript has many constructions which look like well-known constructions in other computer languages. It's dangerous for JavaScript to interpret the construction in another way as most other computer languages.

如果有人不了解JavaScript(顺便说一下常见的情况)看到的结构类似于

If somebody who doesn't know JavaScript good enough (the common case by the way) sees the construction like

for (var i=0; i<100; i++) {
    // your code here
}

或看到变量的声明阻止

{
    var i;
    //some code
    {
        var j;
        // some code
    }
}

然后大多数读者会认为会定义块级变量。 JavaScript没有块级变量。所有变量都将被解释为定义的函数级别。

then most readers will think that block level variables will be defined. JavaScript don't have block level variables. All variables will be interpreted as function level defined.

因此,如果代码不仅仅是一些测试代码,我永远不会在代码中定义变量这将只写5分钟。主要原因是我不想编写代码并使用可能被误解的语言结构

So I never define variables inside of the code if the code is not just some test code which will be written for 5 min only. The main reason is that I don't want to write code and use language constructions which could be misunderstood.

顺便说一下JSLint找到定义块内的变量如此糟糕,以至于它停止了代码分析的处理。没有可以改变此行为的JSLint选项。我发现JSLint的行为不好,但我同意在中为循环声明变量是不好的,因为它会被大多数人读作 local的代码循环变量,这是不正确的。

By the way JSLint finds defining of variables inside of block such bad style that it stop processing of the code analysis. There are no JSLint options which could change this behavior. I find the behavior of JSLint not good, but I agree that declaration of variables inside of for loop is bad because it will be read by most persons as code with local loop variables, which is incorrect.

如果你使用

for (var i=0; i<100; i++) {
    // your code here
}
// some other code here
for (var i=0; i<500; i++) {
    // custom code here
}



<然后,JavaScript为您移动函数开头的所有变量声明。因此代码将为

then JavaScript moves all declarations of variables at the beginning of the function for you. So the code will be as

var i = undefined, i = undefined; // duplicate declaration which will be reduced
                                  // to one var i = undefined;

for (i=0; i<100; i++) {
    // your code here
}
// some other code here
for (i=0; i<500; i++) {
    // custom code here
}

所以请考虑其他读者的代码。不要使用任何可能以错误方式解释的结构。

So please think about other readers of the code. Don't use any constructions which could be interpreted in the wrong way.

这篇关于处理循环变量的重复声明警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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