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 及更早版本中的錯誤,其中 catch 子句參數可能會洩漏到外部範圍。每當此規則遇到與外部範圍中的變數同名的 catch 子句參數時,都會發出警告。
此規則的不正確程式碼範例
在遊樂場中開啟
/*eslint no-catch-shadow: "error"*/
var err = "x";
try {
throw "problem";
}
function error() {
// ...
};
try {
throw "problem";
}
此規則的正確程式碼範例
在遊樂場中開啟
/*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 中引入。