ESLint意外使用isNaN [英] ESLint Unexpected use of isNaN

查看:281
本文介绍了ESLint意外使用isNaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Node.js模块的箭头函数内使用 isNaN 全局函数,但出现此错误:

I'm trying to use the isNaN global function inside an arrow function in a Node.js module but I'm getting this error:

[eslint]意外使用了'isNaN'。 (无限制全局变量)

这是我的代码:

const isNumber = value => !isNaN(parseFloat(value));

module.exports = {
  isNumber,
};

关于我在做什么错的任何想法吗?

Any idea on what am I doing wrong?

PS:我正在使用AirBnB样式指南。

PS: I'm using the AirBnB style guide.

推荐答案

作为文档建议,请使用 Number.isNaN

As the documentation suggests, use Number.isNaN.

const isNumber = value => !Number.isNaN(Number(value));






引用Airbnb的文档:


Quoting Airbnb's documentation:


为什么?全局isNaN将非数字强制转换为数字,对于强制转换为NaN的任何内容,返回true
。如果需要这种行为,则使其显露

Why? The global isNaN coerces non-numbers to numbers, returning true for anything that coerces to NaN. If this behavior is desired, make it explicit.



// bad
isNaN('1.2'); // false
isNaN('1.2.3'); // true

// good
Number.isNaN('1.2.3'); // false
Number.isNaN(Number('1.2.3')); // true

这篇关于ESLint意外使用isNaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆