美文网首页
0, -0, Infinity, NaN

0, -0, Infinity, NaN

作者: videring | 来源:发表于2019-10-11 09:17 被阅读0次
  • Infinity
console.log(Infinity === Infinity) // true
console.log(Infinity === 1 / 0) // true
console.log(-Infinity === -1 / 0) // true
console.log(Infinity === -Infinity) // false
console.log(Object.is(Infinity, Infinity)) // true
console.log(Object.is(Infinity, 1 / 0)) // true
console.log(1 / -Infinity); // -0
console.log(-1 / Infinity); // -0
  • NaN
console.log(NaN===NaN) // false
console.log(NaN===0/0) // false
console.log(Object.is(NaN, NaN));  // true
console.log(Object.is(NaN, 0/0));  // true
console.log(Object.is(NaN, 1/0));  // false
  • +0 -0
console.log(-0) // -0
console.log(0 === -0) // true
console.log(Object.is(0, -0)); // false
console.log(Object.is(0, +0)); // true
console.log(Object.is(-0, -0)); // true
console.log(undefined === void 0) // true, void 运算符能对给定的表达式进行求值,然后返回 undefined
  • 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;

    }

  };

}

相关文章

网友评论

      本文标题:0, -0, Infinity, NaN

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