在 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 的全部内容,希望对大家的学习工作能有所帮助。
来源: http://www.jb51.net/article/91183.htm