no-catch-shadow
禁止 catch
子句参数遮蔽外部作用域中的变量
在 IE 8 及更早版本中,如果 catch 子句参数与外部作用域中的变量同名,则 catch 子句参数可以覆盖该变量的值。
var err = "x";
try {
throw "problem";
} catch (err) {
}
console.log(err) // err is 'problem', not 'x'
规则详情
此规则旨在防止您的程序中出现意外行为,这些行为可能由 IE 8 及更早版本中的一个 bug 引起,在该 bug 中,catch 子句参数可能会泄漏到外部作用域。每当遇到与外部作用域中的变量同名的 catch 子句参数时,此规则都会发出警告。
此规则的错误代码示例
在 Playground 中打开
/*eslint no-catch-shadow: "error"*/
var err = "x";
try {
throw "problem";
}
function error() {
// ...
};
try {
throw "problem";
}
此规则的正确代码示例
在 Playground 中打开
/*eslint no-catch-shadow: "error"*/
var err = "x";
try {
throw "problem";
} catch (e) {
}
function error() {
// ...
};
try {
throw "problem";
} catch (e) {
}
何时不使用
如果您不需要支持 IE 8 及更早版本,则应关闭此规则。
版本
此规则在 ESLint v0.0.9 中引入。