刚开始看源码, 这一行我有点困惑:
// The current target watcher being evaluated.
// This is globally unique because only one watcher
// can be evaluated at a time.
Dep.target = null
const targetStack = []
export function pushTarget (target: ?Watcher) {
targetStack.push(target)
Dep.target = target
}
export function popTarget () {
targetStack.pop()
Dep.target = targetStack[targetStack.length - 1]
}
这一行在export的函数外面。不是只在被import的时候执行一次吗? 后面就再也不能执行到他了, 所以他是怎么起作用的。
你把他看作一个变量,紧接着这行代码,下面导出的两个函数就用到了这个变量,这里形成了一个闭包。