underscore.js 内部方法 eq(a,b,aStack,bStack)-问答-阿里云开发者社区-阿里云

开发者社区> 小旋风柴进> 正文

underscore.js 内部方法 eq(a,b,aStack,bStack)

来自:数字技术前瞻 2016-03-17 13:27:35 2024 1
 // Internal recursive comparison function for `isEqual`.
  var eq = function(a, b, aStack, bStack) {
    // Identical objects are equal. `0 === -0`, but they aren't identical.
    // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
    if (a === b) return a !== 0 || 1 / a === 1 / b;
    // A strict comparison is necessary because `null == undefined`.
    if (a == null || b == null) return a === b;
    // Unwrap any wrapped objects.
    if (a instanceof _) a = a._wrapped;
    if (b instanceof _) b = b._wrapped;
    // Compare `[[Class]]` names.
    var className = toString.call(a);
    if (className !== toString.call(b)) return false;
    switch (className) {
      // Strings, numbers, regular expressions, dates, and booleans are compared by value.
      case '[object RegExp]':
      // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
      case '[object String]':
        // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
        // equivalent to `new String("5")`.
        return '' + a === '' + b;
      case '[object Number]':
        // `NaN`s are equivalent, but non-reflexive.
        // Object(NaN) is equivalent to NaN
        if (+a !== +a) return +b !== +b;
        // An `egal` comparison is performed for other numeric values.
        return +a === 0 ? 1 / +a === 1 / b : +a === +b;
      case '[object Date]':
      case '[object Boolean]':
        // Coerce dates and booleans to numeric primitive values. Dates are compared by their
        // millisecond representations. Note that invalid dates with millisecond representations
        // of `NaN` are not equivalent.
        return +a === +b;
    }

    var areArrays = className === '[object Array]';
    if (!areArrays) {
      if (typeof a != 'object' || typeof b != 'object') return false;

      // Objects with different constructors are not equivalent, but `Object`s or `Array`s
      // from different frames are.
      var aCtor = a.constructor, bCtor = b.constructor;
      if (aCtor !== bCtor && !(_.isFunction(aCtor) && aCtor instanceof aCtor &&
                               _.isFunction(bCtor) && bCtor instanceof bCtor)
                          && ('constructor' in a && 'constructor' in b)) {
        return false;
      }
    }
    // Assume equality for cyclic structures. The algorithm for detecting cyclic
    // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
    var length = aStack.length;
    while (length--) {
      // Linear search. Performance is inversely proportional to the number of
      // unique nested structures.
      if (aStack[length] === a) return bStack[length] === b;
    }

    // Add the first object to the stack of traversed objects.
    aStack.push(a);
    bStack.push(b);
    var size, result;
    // Recursively compare objects and arrays.
    if (areArrays) {
      // Compare array lengths to determine if a deep comparison is necessary.
      size = a.length;
      result = size === b.length;
      if (result) {
        // Deep compare the contents, ignoring non-numeric properties.
        while (size--) {
          if (!(result = eq(a[size], b[size], aStack, bStack))) break;
        }
      }
    } else {
      // Deep compare objects.
      var keys = _.keys(a), key;
      size = keys.length;
      // Ensure that both objects contain the same number of properties before comparing deep equality.
      result = _.keys(b).length === size;
      if (result) {
        while (size--) {
          // Deep compare each member
          key = keys[size];
          if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break;
        }
      }
    }
    // Remove the first object from the stack of traversed objects.
    aStack.pop();
    bStack.pop();
    return result;
  };

  // Perform a deep comparison to check if two objects are equal.
  _.isEqual = function(a, b) {
    return eq(a, b, [], []);
  };
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation JO.
var length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if (aStack[length] === a) return bStack[length] === b;
}

这几段代码在什么情况下会进入if条件语句 请教高人给个例子 ,谢谢!

取消 提交回答
全部回答(1)
相关问答

7

回答

【漏洞公告】CVE-2016-5195“脏牛(Dirty COW)”漏洞- Linux内核通杀提权漏洞

英鸷 2016-10-22 17:10:15 25312浏览量 回答数 7

2

回答

云栖小镇•云计算生态-《凌云》2014.11总第五期

tech君 2014-11-05 15:54:28 17089浏览量 回答数 2

15

回答

免费Docker镜像来袭,你造么?

豆妹 2014-11-21 15:49:57 23200浏览量 回答数 15

59

回答

【有话“值”说】说出你对自助服务的感受或期待 众多奖品搬回家

阿里云支持与服务 2016-07-19 13:41:07 21943浏览量 回答数 59

7

回答

他用20年写下1亿行代码 神级程序员编程必备歌单

元芳啊 2016-07-18 15:13:21 18020浏览量 回答数 7

10

回答

新的技术,比如大数据、AI如何应用在航空行业?

诺诺罗亚 2019-06-26 16:15:54 117474浏览量 回答数 10

22

回答

专家教你:省钱更省心,用镜像0元购云服务器!

豆妹 2014-08-27 17:03:25 43032浏览量 回答数 22

87

回答

亮出你的专属二次元形象,领天猫精灵音箱

霸道网管 2021-01-08 09:08:10 21138浏览量 回答数 87

5

回答

2018云栖大会·重庆峰会

游客886 2018-07-27 10:57:51 26733浏览量 回答数 5

55

回答

『开发者大会.2013』伙伴们,快准备素材,登上“云梦想墙”!

qiujin2012 2013-09-13 21:46:41 31249浏览量 回答数 55
2728
文章
6591
问答
来源圈子
更多
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载