请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

关于cleanDeps的一个疑问

 cleanupDeps () {
    let i = this.deps.length
    while (i--) {
      const dep = this.deps[i]
      if (!this.newDepIds.has(dep.id)) {
        dep.removeSub(this)
      }
    }
    let tmp = this.depIds
    this.depIds = this.newDepIds
    this.newDepIds = tmp
    this.newDepIds.clear()
    tmp = this.deps
    this.deps = this.newDeps
    this.newDeps = tmp
    this.newDeps.length = 0
  }

为什么前面遍历了this.deps,去除掉了不在

 if (!this.newDepIds.has(dep.id)) {
        dep.removeSub(this)
      }

后面还要这样处理?

this.deps = this.newDeps

正在回答 回答被采纳积分+3

2回答

鸡肋2016 2019-10-27 15:31:50

if (!this.newDepIds.has(dep.id)) {
       dep.removeSub(this)
     }

因为这个代码的目的是去除 dep中这次是否使用到了(或者说依赖到了)watcher ,如果这次(渲染)没有使用到 这个dep ,那么dep的内容中也去除掉这个watcher ,方便下一次set更新的时候 不再通知这个watcher

0 回复 有任何疑惑可以回复我~
ustbhuangyi 2018-10-07 21:58:32

tmp = this.deps
this.deps = this.newDeps
this.newDeps = tmp

这几行代码的作用是交换 this.deps 和 this.newDeps,目的是更新一下 this.deps,作为每次保存的 deps

0 回复 有任何疑惑可以回复我~
  • 这里我也还不太明白。
    tmp = this.deps
    this.deps = this.newDeps
    this.newDeps = tmp
    this.newDeps.length = 0
    
    这里是要更新this.deps,那么为什么上面还要对this.deps进行操作呢?剔除不必要watcher后的this.deps不是直接被this.newDeps覆盖了么。而且while里并没有对this.newDeps进行逻辑操作。
    回复 有任何疑惑可以回复我~ 2020-03-18 22:30:13
  • 懂了,这里的this.deps只是个容器,while里修改的是内部dep和它没关系
    回复 有任何疑惑可以回复我~ 2020-03-19 11:12:36
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

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

帮助反馈 APP下载

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

公众号

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