jiefancis / blogs

个人博客,学习笔记(issues)
1 stars 0 forks source link

如何解决watch对象新值与旧值相等 #20

Open jiefancis opened 2 years ago

jiefancis commented 2 years ago

// core/instance/watcher class Watcher { constructor( vm, expOrFn, cb, options) { this.getter = typeof expOrFn === 'function' ? expOrFn : parsePath(expOrFn) this.cb = cb this.immdiate = options.immdiate this.deep = options.deep

     // 旧值
    this.value = this.lazy ? undefined : this.get()
 }

  get () {
      // getter是我们传入的返回深拷贝对象的函数
       return this.getter(vm,vm)
  }
  run() {
       let value = this.get()
       let oldValue = this.value
       if(
             value  !== oldValue ||
             this.deep
         ) {
           this.cb(value, oldValue)
         }
  }

}