no-catch-shadow

禁止 catch 子句参数来自外部作用域的阴影变量

Important

This rule was deprecated in ESLint v5.1.0. Please replace the rule with no-shadow.

Learn more

在 IE 8 和更早版本中,如果该变量与 catch 子句参数同名,则 catch 子句参数可以覆盖外部范围内的变量的值。

¥In IE 8 and earlier, the catch clause parameter can overwrite the value of a variable in the outer scope, if that variable has the same name as the catch clause parameter.

var err = "x";

try {
    throw "problem";
} catch (err) {

}

console.log(err)    // err is 'problem', not 'x'

规则详情

¥Rule Details

此规则旨在防止程序中的意外行为,这些行为可能由 IE 8 及更早版本中的错误引起,其中 catch 子句参数可能会泄漏到外部范围。每当遇到与外部作用域中的变量同名的 catch 子句参数时,此规则都会触发警告。

¥This rule is aimed at preventing unexpected behavior in your program that may arise from a bug in IE 8 and earlier, in which the catch clause parameter can leak into outer scopes. This rule will warn whenever it encounters a catch clause parameter that has the same name as a variable in an outer scope.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-catch-shadow: "error"*/

var err = "x";

try {
    throw "problem";
} catch (err) {

}

function error() {
    // ...
};

try {
    throw "problem";
} catch (error) {

}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-catch-shadow: "error"*/

var err = "x";

try {
    throw "problem";
} catch (e) {

}

function error() {
    // ...
};

try {
    throw "problem";
} catch (e) {

}

何时不使用

¥When Not To Use It

如果你不需要支持 IE 8 及更早版本,则应关闭此规则。

¥If you do not need to support IE 8 and earlier, you should turn this rule off.

版本

此规则是在 ESLint v0.0.9 中引入。

资源