1.为什么初始化会执行computFn
2.为什么点击按钮会执行computFn和watch
找到答案了...
1.为watch中的computFn创建实例化用户watcher的时候,访问到了computed中的computFn执行了computedGetter,首次watcher.dirty为true执行了watcher.evaluate,所以初始化会执行computFn,并且给num添加了计算watcher和用户watcher。
2.点击按钮改变num的值,通知订阅者们进行更新,先将watcher的dirty置为true,然后因为执行this.get,所以执行了computed的computFn;执行this.cb.call其实就是在执行watch的computFn
初始化时,因为你
watch
了computFn
所以会去计算 就会执行computFn
。当你点击按钮之后
num
改变,computFn
就会变watch
当然会执行了。