Eslint
规则 | Rules

no-compare-neg-zero

不允许与-0比较(no-compare-neg-zero)

配置文件中的"extends": "eslint:recommended"属性启用此规则。

规则细节

该规则应该针对试图与-0进行比较的代码发出警告,因为这不会按预期工作。也就是说,像x === -0这样的代码将通过+0和-0。作者可能打算 Object.is(x,-0)。

此规则的错误代码示例:

if (x === -0) { // doSomething()... }

此规则的正确代码示例:

if (x === 0) { // doSomething()... }

if (Object.is(x, -0)) { // doSomething()... }

版本

该规则在 ESLint 3.17.0中引入。

资源