美文网首页
Object.is和==,===

Object.is和==,===

作者: 焦糖大瓜子 | 来源:发表于2019-06-04 17:03 被阅读0次
  1. ES5比较值得问题:
  • “==”
    会进行数据类型转换
  • “===”
    +0 === -0 // false
    NaN === NaN // false
  1. ES6提出同值相等算法,"Same-value equality",用来比较两个值是否严格相等
Object.is({},{}); // false
Object.is(+0, -0); // false
Object.is(NaN,NaN); // true
  1. 使用ES5实现Object.is
Object.defineProperty(Object, 'is', {
  value: function(x,y){
    if(x === y){
      // 不能让 + 0 === -0
      return x !== 0 || 1/x === 1/y
    }

    return x!==x && y !== y;
  },
  configurable: true,
  enumerable: false,
  writable: true
})```

相关文章

网友评论

      本文标题:Object.is和==,===

      本文链接:https://www.haomeiwen.com/subject/vxqftctx.html