henrycjchen / blog

henry's blog
https://henrycjchen.github.io/blog/docs/
0 stars 0 forks source link

vue: 依赖收集 #19

Open henrycjchen opened 4 years ago

henrycjchen commented 4 years ago
computed: {
  a() {
    return this.b
  }
}

依赖收集时,针对 a,先将 key a 放到全局如 window.depKey = 'a',执行函数 a,此时函数中 return this.b 会触发 b defineProperty 中的 get 方法,并将 window.depKey 放到 b 的依赖列表中。

此后,当 b 修改时,便会遍历触发 b 的依赖列表的 key 的数据更新