为了账号安全,请及时绑定邮箱和手机立即绑定

immutability-helper 源码的疑惑

immutability-helper 源码的疑惑

饮歌长啸 2019-03-05 13:15:04
在看immutability-helper的源码,对其中一段函数很疑惑  update.isEquals = function(a, b) { return a === b; };  function update(object, spec) {    if (typeof spec === 'function') {      return spec(object);    }    if (!(Array.isArray(object) && Array.isArray(spec))) {      invariant(        !Array.isArray(spec),        'update(): You provided an invalid spec to update(). The spec may ' +        'not contain an array except as the value of $set, $push, $unshift, ' +        '$splice or any custom command allowing an array value.'      );    }    invariant(      typeof spec === 'object' && spec !== null,      'update(): You provided an invalid spec to update(). The spec and ' +      'every included key path must be plain objects containing one of the ' +      'following commands: %s.',      Object.keys(commands).join(', ')    );    var nextObject = object;    var index, key;    getAllKeys(spec).forEach(function(key) {      if (hasOwnProperty.call(commands, key)) {        var objectWasNextObject = object === nextObject;        nextObject = commands[key](spec[key], nextObject, spec, object);        // update.isEquals(nextObject, object) 为true那不就是 nextObject 一定等于 object, 再写 nextObject = object 有什么意义        if (objectWasNextObject && update.isEquals(nextObject, object)) {          nextObject = object;        }      } else {        var nextValueForKey =          type(object) === 'Map'            ? update(object.get(key), spec[key])            : update(object[key], spec[key]);        if (!update.isEquals(nextValueForKey, nextObject[key]) || typeof nextValueForKey === 'undefined' && !hasOwnProperty.call(object, key)) {          if (nextObject === object) {            nextObject = copy(object);          }          if (type(nextObject) === 'Map') {            nextObject.set(key, nextValueForKey);          } else {这段函数有什么意义? update.isEquals(nextObject, object)不就表示两者相等了if (objectWasNextObject && update.isEquals(nextObject, object)) {          nextObject = object;}
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

isEquals为暴露的接口,用户可以自身定义 myUpdate.isEquals = ...
比如将isEquals定义为深度检查,那仍将nextObject = object

查看完整回答
反对 回复 2019-04-04
  • 1 回答
  • 0 关注
  • 418 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号