Giter VIP home page Giter VIP logo

vue2-learn's People

Watchers

 avatar

vue2-learn's Issues

vm._watcher和vm._watchers的区别是什么?

_watcher是一个渲染watcher,他是在forceupdate中会触发,
image
当$data变化时,会触发setdata
image
setdata最后一步就是forceupdate,
这个地方本不应该改变$data,但是确实被改变了,所以会触发forceupdate更新。
image
fforceupdate实际上调用了this._watcher.run()方法,去更新视图。
所以很容易理解,就是用户触发了一些不应该被更新的内容,为了保证响应式,触发了forceupdate,forceupdate依靠this,_watcher的run方法去实现。就是这样。
就是本不应该被改变的数据被改变了,导致视图需要相应变化,于是调用了watcher。
所以可以总结一下,watcher是用来更新视图的。
至于_watchers是普通watcher,是基于watch这个属性里面定义的watcher,因此,主要用于执行回调函数。

在observer/index.js中,在定义响应式时,为什么要先获取变量已有的get和set

image
zhesh这是获取get和set的地方,

image
这个地方,触发了之前的getter,

之所以要触发之前的getter,是因为响应式对象的属性可能本来已经被用户定义为一个get和set,因此在触发vue的get的时候,要把用户自定义的get和set触发一下,以免漏掉逻辑。

这里可以看到,定义响应式核心其实,就是在get的时候,通知watcher收集依赖。在set的时候,通知,watcher去更新。

顺便提一下,dep和watcher是一个相互依赖的逻辑,dep通过addsub依赖于watcher,watcher通过depend方法依赖于dep。dep依赖watcher,是为了notify每个watcher,watcher依赖dep是为了好在将来,去掉dep中依赖的watcher。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.