eslint/no-eq-null 限制
功能說明
禁止在沒有類型檢查運算子的情況下比較 null
。
為何不好?
在沒有類型檢查運算子(==
或 !=
)的情況下與 null
進行比較,可能會產生意想不到的結果,因為當與不僅是 null
,還有 undefined
值進行比較時,比較結果將評估為 true
。
範例
此規則的錯誤程式碼範例
js
if (foo == null) {
bar();
}
if (baz != null) {
bar();
}
此規則的正確程式碼範例
js
if (foo === null) {
bar();
}
if (baz !== null) {
bar();
}
if (bang === undefined) {
bar();
}