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

以上所述就是本文的全部内容了,希望大家能够喜欢。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3