-1

When I use Closure Compiler I get the following errors when compiling in advanced mode: (in simple- and Whitespace only mode the code gets not problems at all)

JSC_REDECLARED_VARIABLE: Redeclared variable: e at line 31 character 9
} catch (e) {
         ^
JSC_REDECLARED_VARIABLE: Redeclared variable: e at line 34 character 9
} catch (e) {
         ^

The code described by Closure Compiler as an error is the following:

function getXMLHttp() {
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                return false;
            }
        }
    }
    return xmlHttp;
}

Why this problem occurs when I use Advanced mode, I don't know. I have no idea why the compiler says this is an error as well.

(Quick note: This code is just a simple XMLrequest for an PHP file in a error catch handling for handling IE. All my JavaScript code works as it should be.)

JJJ
  • 32,902
  • 20
  • 89
  • 102
Jason Stackhouse
  • 1,796
  • 2
  • 18
  • 19

2 Answers2

2

You can easily get rid of it by using e, e2 and e3 for your exceptions. That's dirty but still a good workaround.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

The way you use e, there will be no problem and your code should work as expected, however, if you would want to do something like this:

try {
    xmlHttp = new XMLHttpRequest();
} catch (e) {
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            return false;
        }
    }
    console.log(e); // Now there is a chance that e refer to the "wrong" exception
}

The closure compiler anticipates this, and throws a warning about it, since you re-declare e for every catch-statement.

As ThiefMaster suggest, you could simply rename the variables to e2, e3 and so forth if you want to get rid of the warning.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103