Vue.js源码解析一
依赖追踪
前三章主要讲解的是响应式系统,vue侦测数据的变化,当数据变化时,会通知视图进行响应的更新。
侦测一个对象的变化 Object.defineProperty(后面会使用ES6的Proxy)
Object.defineProperty(obj,key,{
// 这里都有固定的含义 javascript中自己查找含义
// enumerable: false,
// writable: true,
// configurable: true,
// 每当从obj的key中读取数据时,get函数被触发,修改key数据时,set被触发
get() {
console.log(`getting key "${key}": ${oldvalue}`)
return oldvalue
},
set(newvalue) {
console.log(`setting key "${key}" to: ${newvalue}`)
oldvalue = newvalue
}
})
先收集依赖,把用到数据的地方收集起来,然后等属性发生变化时,把之前收集好的依赖循环触发一遍,即getter中收集依赖,setter中触发依赖
个人感觉书籍上讲解的很麻烦,还是尤雨溪作者本人讲解的简易版本比较清楚
getter和setter函数重写
dependency-tracking
mini-observer
observe()converts修改 the properties in the received object and make them reactive. For each converted property, it gets assigned aDepinstance which keeps track of a list of subscribing update functions, and triggers them to re-run when its setter is invoked.autorun()takes an update function and re-runs it when properties that the update function subscribes to have been mutated修改. An update function is said to be “subscribing” to a property if it relies on that property during its evaluation.
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 邹阳 の 博客!


