浅析Javascript ES6新增值比较函数Object.is

在Object.is出现之前我们比较值使用两等号 “==” 或 三等号“===”, 三等号更加严格,只要比较两方类型不同立即返回false。

另外,有且只有一个值不和自己相等,它是NaN

 

现在ES6又加了一个Object.is,让比较运算的江湖更加混乱。

多数情况下Object.is等价于“===”,如下

1 === 1 // true
Object.is(1, 1) // true
 
'a' === 'a' // true
Object.is('a', 'a') // true
 
true === true // true
Object.is(true, true) // true
 
null === null // true
Object.is(null, null) // true
 
undefined === undefined // true
Object.is(undefined, undefined) // true

但对于NaN、0、+0、 -0,则和 “===” 不同

NaN === NaN // false
Object.is(NaN, NaN) // true
 
0 === -0 // true
Object.is(0, -0) // false
 
-0 === +0 // true
Object.is(-0, +0) // false

以上就是关于Javascript ES6新增值比较函数Object.is的全部内容,希望对大家的学习工作能有所帮助。