const sharedPropertyDefinition = {
export function defineComputed (
userDef: Object | Function
const shouldCache = !isServerRendering() // true
if (typeof userDef === 'function') {
sharedPropertyDefinition.get = shouldCache
? createComputedGetter(key)
: createGetterInvoker(userDef)
sharedPropertyDefinition.set = noop
Object.defineProperty(target, key, sharedPropertyDefinition)
function createComputedGetter (key) {
return function computedGetter () {
const watcher = this._computedWatchers && this._computedWatchers[key]
// 第一次会触发依赖收集, watcher.deps 是当前的所有依赖
// 第一次上面执行完后,当前Dep.target是当前Render!
// 把当前computed watcher的全部作为依赖都逐一添加到render watcher
// 因此逐一依赖的subs 也会有render watcher
// 那么所有依赖的subs 不仅有了computed watcher
// 没有触发视图渲染的computed不会触发get,也不会到这里
// 甚至虽然computed-watch触发,但是最终返回值不变