javascript Object.is 与 === 的区别

来源:互联网 发布:计算机二级vb视频教程 编辑:程序博客网 时间:2024/06/05 20:35

Object.is与===基本相同,只在两个地方有差异,+0 -0 ,NaN和NaN

Object.is 的 Polyfill 实现

if (!Object.is) {  Object.is = function(x, y) {    // SameValue algorithm    if (x === y) { // Steps 1-5, 7-10      // Steps 6.b-6.e: +0 != -0      return x !== 0 || 1 / x === 1 / y;    } else {     // Step 6.a: NaN == NaN     return x !== x && y !== y;    }  };}

Object.is() method is not the same as being equal according to the === operator. The === operator (and the == operator as well) treats the number values -0 and +0 as equal and treats Number.NaN as not equal to NaN.

参考文档
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
http://stackoverflow.com/questions/30543190/object-is-vs

0 0
原创粉丝点击