Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⛽️第5期第2题:介绍一下NaN并实现一个isNaN #31

Open
LinDaiDai opened this issue Jun 23, 2020 · 0 comments
Open

⛽️第5期第2题:介绍一下NaN并实现一个isNaN #31

LinDaiDai opened this issue Jun 23, 2020 · 0 comments

Comments

@LinDaiDai
Copy link
Owner

介绍一下NaN并实现一个isNaN

介绍一下NaN

  • NaN属性是代表非数字值的特殊值,该属性用于指示某个值不是数字;
  • NaN是不等于NaN的,即NaN === NaN的结果是false
  • 使用Object.is()来比较两个NaN结果是true,即Object.is(NaN, NaN)的结果是true
  • typeof NaN"number"
  • 方法parseInt()parseFloat()在不能解析指定的字符串时就返回这个值;
  • 可以使用isNaN来判断一个变量是不是NaN,它是JS内置对象Number上的静态方法。

(关于第三点,大家可以看一下我之前的一篇文章哟,里面的「第二补:JS类型检测-Object.is()和===的区别」有提到:读《三元-JS灵魂之问》总结,给自己的一份原生JS补给(上))

实现一个isNaN:

对于isNaNpolyfill实现起来就比较简单了,只需要利用NaN不等于它自身的这一点即可:

const isNaN = v => v !== v;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant