Heim  >  Fragen und Antworten  >  Hauptteil

Sind in JavaScript mit Ausnahme von NaN alle anderen Variablen gleich sich selbst?

Wie ermittelt JavaScript schnell, ob der Wert einer Variablen val NaN ist? Wenn mit Ausnahme des NaN-Werts alle anderen Variablen gleich sich selbst sind, reicht es aus, direkt zu beurteilen, ob val===val?

学习ing学习ing2633 Tage vor1380

Antworte allen(4)Ich werde antworten

  • 怪我咯

    怪我咯2017-07-05 10:52:13

    NaN是JavaScript里面唯一一个非自反的值,也就是

    NaN === NaN // false
    

    这本书里面有提到:

    NaN is a very special value in that it's never equal to another NaN value (i.e., it's never equal to itself). It's the only value, in fact, that is not reflexive (without the Identity characteristic x === x). So, NaN !== NaN.

    你不知道的JavaScript

    Antwort
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:52:13

    是的,NaN 使用 isNaN 或者 是否等于自己来判断

    同时,反过来说,如果两个变量相等判断这两个变量是否相同有 +0 和 -0 的特殊情况,使用倒数判断是否相等。

    Antwort
    0
  • 阿神

    阿神2017-07-05 10:52:13

    let a = []
    let b = []
    
    console.log(a===b)

    题主试一试?

    Antwort
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-05 10:52:13

    ES6在Number对象上,新提供了Number.isNaN()方法,建议直接使用Number.isNaN()来检查一个值是否为NaN
    另外除开 +0-0 的情况,===是满足需求的,更好一点的方式使用Object.is()

    Antwort
    0
  • StornierenAntwort