CyanSalt / notebook

3 stars 0 forks source link

调用 JavaScript 底层的 SameValue() 实现 Object.is() #4

Open CyanSalt opened 6 years ago

CyanSalt commented 6 years ago

path: es5-object-is


/**
 * Inspired by Allen Wirfs-Brock
 * @see {https://tc39.github.io/ecma262/#sec-samevalue}
 */
function is(a, b) {
  var target = {}
  Object.defineProperty(target, '_', {value: a, configurable: false})
  try {
    Object.defineProperty(target, '_', {value: b})
  } catch (e) {
    return false
  }
  return true
}
is(1, 1) // true
is(1, 2) // false
is(0, 0) // true
is(0, -0) // false
is(NaN, NaN) // true
is([], []) // false
is({}, {}) // false
is({}, {}) // false
is(Object.prototype, Object.prototype) // true
is(null, null) // true
is() // true