ES6新增值比较函数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

 

相关:

Object.is

别了,JavaScript中的isXX系列

世界上最短的数字判断代码

JavaScript中奇葩的假值

posted on 2015-05-07 16:30  snandy  阅读(5290)  评论(4编辑  收藏  举报