_.eq(value, other)

123

_.eq(value, other)

_.eq判断两个值是否相等,遵循SameValueZero规则

参数

object (Object): 需要检查的对象
source (Object): 检查方法predicate组成的对象

返回值

(boolean): 如果检查通过返回true,否则false

例子

var object = { 'a': 1 };
var other = { 'a': 1 };
 
_.eq(object, object);
// => true
 
_.eq(object, other);
// => false
 
_.eq('a', 'a');
// => true
 
_.eq('a', Object('a'));
// => false
 
_.eq(NaN, NaN);
// => true

源代码

/**
 * Performs a
 * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
 * comparison between two values to determine if they are equivalent.
 *
 * @since 4.0.0
 * @category Lang
 * @param {*} value The value to compare.
 * @param {*} other The other value to compare.
 * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
 * @example
 *
 * const object = { 'a': 1 }
 * const other = { 'a': 1 }
 *
 * eq(object, object)
 * // => true
 *
 * eq(object, other)
 * // => false
 *
 * eq('a', 'a')
 * // => true
 *
 * eq('a', Object('a'))
 * // => false
 *
 * eq(NaN, NaN)
 * // => true
 */
//判断两个值是否相等,遵循SameValueZero规则
function eq(value, other) {
  return value === other || (value !== value && other !== other)
}

export default eq

 

posted @ 2018-12-17 18:44  hahazexia  阅读(765)  评论(0)    收藏  举报