vue计算属性Computed的小秘密

vue中computed小秘密的发现之旅

首先我们看一段代码

<body>
    <div id="app">
        {{ count }}
    </div>
</body>
<script>
    new Vue({
        el: '#app',
        data () {
            return {
                num: 66
            }
        },
        computed: {
            count () {
                console.log(1)
                return this.num
            }
        },
        methods: {
            add () {
                setInterval(() => {
                    this.num ++
                }, 1000) 
            }
        },
        created () {
            this.add()
        }
    })
</script>
复制代码

请问

  • console.log(1)会间隔的打印出来吗?
  • html中去掉{{ count }},再问console.log(1)会间隔的打印出来吗?
  • 如果第二问没有打印出来,那么在第二问的基础上怎么修改才能再次打印出来呢?

我先来揭晓答案

  • 会打印出来
  • 不会打印出来
  • 可以用过添加watch监听count,来打印`console.log(1)
    watch: {
        count: function (oldValue, newValue) {

        }
    }
复制代码

请问为什么呢?

以下是我的理解,有误还请指出,共同进步

  • 一句话总结就是computed是惰性求值,即仅仅定义computed的话是没有进行计算属性count的依赖收集(可以类似看成data中的数值,仅仅进行了响应式get,set的定义,并没有触发dep.depend,所以当值发生变化的时候,他并不知道要通知谁,也就不会执行相应的回调函数了;还有就是计算属性的更新,其实就是执行this.get(),就会触发计算属性的function) 源码中有这么一段:
depend () {
  if (this.dep && Dep.target) {  //因为惰性求值,所以Dep.target为false
    this.dep.depend()
  }
}
复制代码

所以如果仅仅是computed的初始化的话并Dep.target就是undefined,所以实例化的watch并不会加入dep的中

看看Computed的实现

  • computed初始化
function initComputed (vm: Component, computed: Object) {
    const watchers = vm._computedWatchers = Object.create(null)  //(标记1)新建一个没有原型链的对象,用来存`computed`对象每个值的watch实例对象
    const isSSR = isServerRendering()  //与服务端渲染有关,暂时忽略
    for (const key in computed) {
        const userDef = computed[key]  //取key的值,该值大部分是function类型
        //下面主要作用就是在非生产环境中没有getter,保警告
        const getter = typeof userDef === 'function' ? userDef : userDef.get
        if (process.env.NODE_ENV !== 'production' && getter == null) {
          warn(
            `Getter is missing for computed property "${key}".`,
            vm
          )
        }
    }
    if (!isSSR) {
      //computed中不同的key,也就是计算属性生成watch实例,
      //watch作用:简单看就是当值发生变化时会触通知到watch,触发更新,执行回调函数
      watchers[key] = new Watcher(
        vm,
        getter || noop,
        noop,
        computedWatcherOptions
      )
    }
    if (!(key in vm)) {
      //作用是将{key: userDef}变成响应式,重写其get和set
      defineComputed(vm, key, userDef)
    } else if (process.env.NODE_ENV !== 'production') {
      if (key in vm.$data) {
        warn(`The computed property "${key}" is already defined in data.`, vm)
      } else if (vm.$options.props && key in vm.$options.props) {
        warn(`The computed property "${key}" is already defined as a prop.`, vm)
      }
    }
}
复制代码
  • defineComputed 先看这个函数做了什么
const sharedPropertyDefinition = {
  enumerable: true,
  configurable: true,
  get: noop,
  set: noop
}
export function defineComputed (
  target: any,
  key: string,
  userDef: Object | Function
) {
    const shouldCache = !isServerRendering()
    if (typeof userDef === 'function') {
      sharedPropertyDefinition.get = shouldCache
        ? createComputedGetter(key)
        : userDef
      sharedPropertyDefinition.set = noop
    } else {
      sharedPropertyDefinition.get = userDef.get
        ? shouldCache && userDef.cache !== false
          ? createComputedGetter(key)
          : userDef.get
        : noop
      sharedPropertyDefinition.set = userDef.set
        ? userDef.set
        : noop
    }
  Object.defineProperty(target, key, sharedPropertyDefinition)
}
复制代码

上面函数的作用就是改写get与set,关键就是这个createComputedGetter在做什么? 早版本createComputedGetter的实现是:

function createComputedGetter(){
    return function computedGetter () {
        //这个就是之前用来收集watch实例的一个对象,可看注释:标记1
        const watcher = this._computedWatchers && this._computedWatchers[key]
        if(watcher) {
            if(watcher.dirty) {
                watcher.evaluate()
            }
            if(Dep.target){ //这里也可以看出Dep.target为false时是不会触发depend,即添加依赖
                watcher.depend()
            }
            return watcher.value
        }
    }
}
复制代码

重点看看watch

export default class Watcher {

  constructor (
    vm: Component,
    expOrFn: string | Function,
    cb: Function,
    options?: ?Object,
    isRenderWatcher?: boolean
  ) {
    //进行初始化的定义,忽略无关代码
      if(options) {
          this.lazy = !!options.lazy
      }else {
          this.lazy = false
      }
      this.getter = parsePath(expOrFn) //返回一个取data值得函数
      this.dirty = this.lazy   //true
      this.value = this.lazy ? undefined : this.get()  //undefined,当不会执行get时也就不会触发get实例方法中的depend的了
    }

  get () {
    // 伪代码
    Dep.target = this
    //取值也就是访问触发属性的get,get中又触发dep.depend(),而dep.depend内部触发的是Dep.target.addDep(this),这里的this其实是Dep实例
    let value = this.getter.call(vm, vm) 
    Dep.target = undefined
  }

  addDep (dep: Dep) {
    //伪代码
    const id = dep.id
    if(!this.depIds.has(id)) {
        this.depIds.add(id)
        this.deps.push(dep)
        dep.addSub(this)  //this是watch实例对象
    }
  }

  update () {
    // 省略...
  }

  getAndInvoke (cb: Function) {
    // 省略...
  }

  evaluate () {
    this.value = this.get()
    this.dirty = false
  }

  depend () {
    let i = this.deps.length
    while(i --) {
        this.deps[i].depend()
    }
  }
  ...

}

复制代码

总结: 1.watcher.dirty默认为true,执行watcher.evaluate()所以computed第一次默认会渲染,与watch不同;2.当默认渲染,触发了get,Dep.target就不是false,就会执行watcher.depend()

watcher.depend() 早版的实现,它有什么问题

  • this.dep这个数组中元素都是Dep的实例对象,watcher所依赖的所有Dep实例化列表; 举个例子:当计算属性中return this.num + this.num1,当读取计算属性时会分别触发num与num1的get,get中又触发dep.depend(),而dep.depend内部触发的是Dep.target.addDep(this),这里的this其实是Dep实例,这样就会分别将不同编号的num与num1的dep,加入到deps中,最后将计算属性的依赖加入到num,num1的Dep中,this.deps[i].depend()也会加,但之前已加入改id所以猜测会直接return掉

  • 这样当num发生改变,触发set,触发其notify方法即遍历dep.subDeps数组(subDeps中放的是各种依赖),触发依赖的update方法。但之前的update方法看了一下

update () {
    /* istanbul ignore else */
    if (this.lazy) {
      this.dirty = true
    } else if (this.sync) {
      this.run()
    } else {
      queueWatcher(this)
    }
  }
}  
复制代码

可以看出直接走queueWatcher(this)所以就算内容没有变化,也会走渲染流程,这就造成了浪费

新版本,发生了变化

  • 第一个createComputedGetter
function createComputedGetter (key) {
  return function computedGetter () {
    const watcher = this._computedWatchers && this._computedWatchers[key]
    if (watcher) {
      watcher.depend()
      return watcher.evaluate()
    }
  }
}
复制代码
  • 第二个watcher.depend()
  if (this.dep && Dep.target) {  
    this.dep.depend()
  }
}
复制代码

上面这里的dep又是哪里来的呢?在watch类中加了下面代码

if (this.computed) {
    this.value = undefined
    this.dep = new Dep()   //类似一个Object对象,进行observer设置get,set响应式时会进let dep = new Dep, 来收集改值得依赖
  } else {
    this.value = this.get()
  }
复制代码

所以从上面的实现可以看出,对当前计算属性自身也生成一个dep列表进行收集;完全可以把一个computed的初始化看出data中数据的初始化,只不过该值又依赖多个依赖

  • 第三个evaluate
evaluate () {
  if (this.dirty) {
    this.value = this.get()
    this.dirty = false
  }
  return this.value
}
复制代码
  • 关键的update也做了修改,
update () {
  /* istanbul ignore else */
  if (this.computed) {
    if (this.dep.subs.length === 0) {
      this.dirty = true
    } else {
      this.getAndInvoke(() => {
        this.dep.notify()
      })
    }
  } else if (this.sync) {
    this.run()
  } else {
    queueWatcher(this)
  }
},
//当计算属性的值发生变化时,改触发回调函数或者进行渲染,而不是通过之前值(例如num改变)变化就触发回调
getAndInvoke (cb: Function) {
    const value = this.get()
    if (
      value !== this.value ||
      isObject(value) ||
      this.deep
    ) {
      const oldValue = this.value
      this.value = value
      this.dirty = false
      if (this.user) {
        try {
          cb.call(this.vm, value, oldValue)
        } catch (e) {
          handleError(e, this.vm, `callback for watcher "${this.expression}"`)
        }
      } else {
        cb.call(this.vm, value, oldValue)
      }
    }
  }
复制代码

**当触发update时首先通过getAndInvoke函数进行值得比较,看是否发生变化,即只有在变化时才会执行,执行的是this.dep.notify(),而这边打的this是当前watch实例对象;因为之前就添加了依赖this.dep.depend()所以接着触发cb.call(this.vm, value, oldValue)cb是:this.dep.notify()但this指向了vm用来触发渲染更新

总结

  • 计算属性的观察者是惰性求值,需要手动通过get
  • 怎么手动get,所以有了问题的第二问,和第三问
  • 触发了get,也就是触发了createComputedGetter函数,就会去取值this.value = this.get(),进行第一次渲染或取值;同时watcher.depend(),将计算属性的依赖添加至dep中,
  • 值发送变化时,输出watch.update,首先判断是否存在依赖,存在则只需watcher.getAndInvoke(cb),

思否也有发布

转载于:https://juejin.im/post/5cdb1208f265da03af27f770

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值