简体   繁体   English

JavaScript:错误 - 已定义变量? 为什么?

[英]JavaScript: Error - variable already defined? Why?

I'm using the awesome JSLint tool to ensure my JavaScript is "strict". 我使用了很棒的JSLint工具来确保我的JavaScript是“严格的”。

When I use it however, I get the following errors: 但是,当我使用它时,我收到以下错误:

'hexRed', 'hexGreen', 'hexBlue', 'color' are already defined (referring to the "else if" clause)

My code is below. 我的代码如下。 Any ideas how to fix my code to make it JavaScript "strict"? 任何想法如何修复我的代码,使其“严格”JavaScript?

function fade(currentStep, numSteps, currentRed, currentGreen, currentBlue, deltaRed, deltaGreen, deltaBlue) {

    if (currentStep < numSteps) {
        var hexRed   = zeroPad(currentRed.toString(16), 2);
        var hexGreen = zeroPad(currentGreen.toString(16), 2);
        var hexBlue  = zeroPad(currentBlue.toString(16), 2);
        var color = "#" + hexRed + hexGreen + hexBlue;

        document.getElementById('abc').style.backgroundColor = color;

        currentRed   += deltaRed;
        currentGreen += deltaGreen;
        currentBlue  += deltaBlue;

        timerID = setTimeout("fade(currentStep, numSteps, currentRed, currentGreen, currentBlue, deltaRed, deltaGreen, deltaBlue)", 70); 

    } else if (currentStep == numSteps) { 

        var hexRed   = endingRed.toString(16);  // <-- JSLint flags this line
        var hexGreen = endingGreen.toString(16);  // <-- JSLint flags this line
        var hexBlue  = endingBlue.toString(16);  // <-- JSLint flags this line
        var color = "#" + hexRed + hexGreen + hexBlue;  // <-- JSLint flags this line

        document.getElementById('abc').style.background = color;
    }
}

JavaScript scopes variables to the function they're in, not to the block between { and }. JavaScript将变量范围限定为它们所在的函数,而不是{和}之间的块。

For example: 例如:

function test(){
   var i=0;
   if (i > 5) {
      var x = i + 1;
      alert(x);
   }
}

actually means: 实际意味着:

function test(){
   var i, x;
   i = 0;
   if (i > 5) {
      x = i + 1;
      alert(x);
   }
}

You can think of is as all variables actually being created with "var" at the top of the function, but initialized to a value where you first assign it. 您可以想到的是,所有变量实际上都是使用函数顶部的“var”创建的,但是初始化为您首先分配它的值。

To fix your issues, simply make this explicit by declaring the variables at the top of your function, like I did in the second code snippet above. 要解决您的问题,只需通过在函数顶部声明变量来明确这一点,就像我在上面的第二个代码片段中所做的那样。

Declare your variables at the top of the function once: 在函数顶部声明一次变量:

function fade(...) {
    var hexRed, hexGreen, hexBlue, color;
    ...

put var hexRed, hexGreen, hexBlue, color var hexRed, hexGreen, hexBlue, color
as the first line in your function, before the if 作为你的函数的第一行,在if之前
remove all other var declarations 删除所有其他var声明

Javascript scoping doesn't work like other c-style languages. Javascript范围与其他c风格的语言不同。 There are only a few possible scope levels- global, function , with , and maybe another odd one or two. 只有少数可能的范围级别 - 全局, functionwith ,也可能是另一个奇怪的一两个。 the if statement does not create a new scope. if语句不会创建新范围。 What you are doing is effectively 你在做什么是有效的

function fade(currentStep, numSteps, currentRed, currentGreen, currentBlue, deltaRed, deltaGreen, deltaBlue) {

    var hexRed;
    var hexGreen;
    var hexBlue;  
    var color;
    var hexRed;
    var hexGreen;
    var hexBlue;  
    var color;


    if (currentStep < numSteps) {
         hexRed   = zeroPad(currentRed.toString(16), 2);
         hexGreen = zeroPad(currentGreen.toString(16), 2);
         hexBlue  = zeroPad(currentBlue.toString(16), 2);
         color = "#" + hexRed + hexGreen + hexBlue;

        document.getElementById('abc').style.backgroundColor = color;

        currentRed   += deltaRed;
        currentGreen += deltaGreen;
        currentBlue  += deltaBlue;

        timerID = setTimeout("fade(currentStep, numSteps, currentRed, currentGreen, currentBlue, deltaRed, deltaGreen, deltaBlue)", 70); 

    } else if (currentStep == numSteps) { 

         hexRed   = endingRed.toString(16);  // <-- JSLint flags this line
         hexGreen = endingGreen.toString(16);  // <-- JSLint flags this line
         hexBlue  = endingBlue.toString(16);  // <-- JSLint flags this line
         color = "#" + hexRed + hexGreen + hexBlue;  // <-- JSLint flags this line

        document.getElementById('abc').style.background = color;
    }
}

which is why jslint is complaining, although your script should still work. 这就是jslint抱怨的原因,尽管你的脚本仍然有效。 This process where the variable declaration is put in the right scope regardless of where the statement exists is known as "hoisting". 无论语句存在于何处,将变量声明放在正确范围内的此过程称为“提升”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM