Skip to content

Commit 6cd1cf4

Browse files
committed
[update]更新注释
1 parent ceac1f7 commit 6cd1cf4

File tree

7 files changed

+54
-26
lines changed

7 files changed

+54
-26
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
vue/.idea/

vue/src/core/instance/state.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const sharedPropertyDefinition = {
3737

3838
/**
3939
* 代理函数,app._data.text -> app.text
40-
* @param {*} target
41-
* @param {*} sourceKey
42-
* @param {*} key
40+
* @param {*} target
41+
* @param {*} sourceKey
42+
* @param {*} key
4343
*/
4444

4545
// proxy(vm, `_props`, key)
@@ -70,6 +70,8 @@ export function initState (vm: Component) {
7070
}
7171

7272
function initProps (vm: Component, propsOptions: Object) {
73+
74+
// 获取props的数据
7375
const propsData = vm.$options.propsData || {}
7476
const props = vm._props = {}
7577
// cache prop keys so that future props updates can iterate using Array
@@ -200,23 +202,7 @@ function initComputed (vm: Component, computed: Object) {
200202

201203
if (!isSSR) {
202204
// create internal watcher for the computed property.
203-
/**
204-
* 熟悉的new Watcher,创建一个订阅者,为了之后收集依赖
205-
* 将例子中的num、lastNum和计算属性comNum进行绑定
206-
* 也就是说在一个deps中有两个dep,其中的subs分别是
207-
* dep1.subs:[watcher(num),watcher(comNum)]
208-
* dep2.subs:[watcher(lastNum),watcher(comNum)]
209-
* dep3........
210-
* 请看前面的例子,页面html中并没有渲染{{lastNum}};按理说就不会执行lastNum的getter
211-
* 从而就不会和计算属性进行关联绑定,如果更改lastNum就不会触发dep2的notify()发布
212-
* 自然也就不会在页面看到comNum有所变化,但是运行后却不是这样,为什么呢
213-
* 这就引出这个initComputed的下面方法了---依赖收集(watcher.prototype.depend)!
214-
* 当时也是看了好久才知道这个depend方法的作用,后面再说
215-
* 首先先来提个头,就是下面代码中watcher中这个getter
216-
* 其实就是function comNum() {return this.num+"-computed-"+this.lastNum;}}
217-
* 而这个getter什么时候执行呢,会在Watcher.prototype.evaluate()方法中执行
218-
* 所以watcher中的evaluate()与depend()两个方法都与initComputed相关
219-
*/
205+
// computed watcher
220206
watchers[key] = new Watcher(
221207
vm,
222208
getter || noop,

vue/src/core/observer/watcher.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export default class Watcher {
171171
// It initializes as lazy by default, and only becomes activated when
172172
// it is depended on by at least one subscriber, which is typically
173173
// another computed property or a component's render function.
174+
// 没有人订阅这个computed watcher的变化,只需要把dirty设置为true,下次有人访问时求值
174175
if (this.dep.subs.length === 0) {
175176
// In lazy mode, we don't want to perform computations until necessary,
176177
// so we simply mark the watcher as dirty. The actual computation is
@@ -181,6 +182,8 @@ export default class Watcher {
181182
// In activated mode, we want to proactively perform the computation
182183
// but only notify our subscribers when the value has indeed changed.
183184
this.getAndInvoke(() => {
185+
186+
// 派发更新
184187
this.dep.notify()
185188
})
186189
}
@@ -233,17 +236,24 @@ export default class Watcher {
233236
*/
234237
evaluate () {
235238
if (this.dirty) {
239+
// 这里的get就是computed上的getter函数
236240
this.value = this.get()
237241
this.dirty = false
238242
}
243+
244+
// 返回getter的值
239245
return this.value
240246
}
241247

242248
/**
243249
* Depend on this watcher. Only for computed property watchers.
244250
*/
245251
depend () {
252+
// Dep.target代表的是渲染watcher
253+
// 在计算计算属性时,触发其他响应式的getter,此时Dep.target代表的是computed watcher
246254
if (this.dep && Dep.target) {
255+
256+
// 渲染watcher订阅computed watcher的变化
247257
this.dep.depend()
248258
}
249259
}

vue/src/core/util/next-tick.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function flushCallbacks () {
1212
pending = false
1313
const copies = callbacks.slice(0)
1414
callbacks.length = 0
15+
16+
// 循环调用callbacks函数
1517
for (let i = 0; i < copies.length; i++) {
1618
copies[i]()
1719
}
@@ -78,6 +80,8 @@ if (typeof Promise !== 'undefined' && isNative(Promise)) {
7880
* Wrap a function so that if any code inside triggers state change,
7981
* the changes are queued using a (macro) task instead of a microtask.
8082
*/
83+
// 强制使用宏任务执行
84+
// 比如对于一些 DOM 交互事件,如 v-on 绑定的事件回调函数的处理,会强制走 macro task
8185
export function withMacroTask (fn: Function): Function {
8286
return fn._withTask || (fn._withTask = function () {
8387
useMacroTask = true
@@ -89,6 +93,8 @@ export function withMacroTask (fn: Function): Function {
8993

9094
export function nextTick (cb?: Function, ctx?: Object) {
9195
let _resolve
96+
97+
// 把传入的函数压入callbacks数组
9298
callbacks.push(() => {
9399
if (cb) {
94100
try {
@@ -109,6 +115,7 @@ export function nextTick (cb?: Function, ctx?: Object) {
109115
}
110116
}
111117
// $flow-disable-line
118+
// 当nextTick不传函数时,提供一个promise化的调用
112119
if (!cb && typeof Promise !== 'undefined') {
113120
return new Promise(resolve => {
114121
_resolve = resolve

vue/src/core/vdom/create-element.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export function createElement (
3737
normalizationType: any,
3838
alwaysNormalize: boolean
3939
): VNode | Array<VNode> {
40+
41+
// 满足data是一个数组或者基础类型时,说明传的是children参数,需要后移一位
42+
// 参数个数的统一性处理
4043
if (Array.isArray(data) || isPrimitive(data)) {
4144
normalizationType = children
4245
children = data
@@ -64,11 +67,11 @@ export function _createElement (
6467
normalizationType?: number
6568
): VNode | Array<VNode> {
6669

67-
/*
68-
如果传递data参数且data的__ob__已经定义(代表已经被observed,上面绑定了Oberver对象),
69-
https://cn.vuejs.org/v2/guide/render-function.html#约束
70-
那么创建一个空节点
71-
*/
70+
/**
71+
* 如果传递data参数且data的__ob__已经定义(代表已经被observed,上面绑定了Oberver对象),
72+
* https://cn.vuejs.org/v2/guide/render-function.html#约束
73+
* 那么创建一个空节点
74+
*/
7275
if (isDef(data) && isDef((data: any).__ob__)) {
7376
process.env.NODE_ENV !== 'production' && warn(
7477
`Avoid using observed data object as vnode data: ${JSON.stringify(data)}\n` +
@@ -79,6 +82,7 @@ export function _createElement (
7982
}
8083

8184
// object syntax in v-bind
85+
// 用在<component is="...">
8286
if (isDef(data) && isDef(data.is)) {
8387
tag = data.is
8488
}
@@ -109,6 +113,8 @@ export function _createElement (
109113
data.scopedSlots = { default: children[0] }
110114
children.length = 0
111115
}
116+
117+
112118
if (normalizationType === ALWAYS_NORMALIZE) {
113119
children = normalizeChildren(children)
114120
} else if (normalizationType === SIMPLE_NORMALIZE) {

vue/src/core/vdom/patch.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ export const emptyNode = new VNode('', {}, [])
3232

3333
const hooks = ['create', 'activate', 'update', 'remove', 'destroy']
3434

35+
/**
36+
* 判断VNode是否一致
37+
* @param a
38+
* @param b
39+
* @returns {boolean|*}
40+
*/
3541
function sameVnode (a, b) {
42+
// key相等
43+
// 标签名、注释、data、input type
44+
// 异步节点
3645
return (
3746
a.key === b.key && (
3847
(
@@ -285,7 +294,7 @@ export function createPatchFunction (backend) {
285294
* dom插入函数
286295
* @param {*} parent - 父节点
287296
* @param {*} elm - 子节点
288-
* @param {*} ref
297+
* @param {*} ref
289298
*/
290299
function insert (parent, elm, ref) {
291300
if (isDef(parent)) {
@@ -721,6 +730,7 @@ export function createPatchFunction (backend) {
721730
createElm(vnode, insertedVnodeQueue)
722731
} else {
723732
const isRealElement = isDef(oldVnode.nodeType)
733+
724734
if (!isRealElement && sameVnode(oldVnode, vnode)) {
725735
// patch existing root node
726736
patchVnode(oldVnode, vnode, insertedVnodeQueue, removeOnly)
@@ -749,11 +759,14 @@ export function createPatchFunction (backend) {
749759
}
750760
// either not server-rendered, or hydration failed.
751761
// create an empty node and replace it
762+
// 把真实 DOM 转成VNode
752763
oldVnode = emptyNodeAt(oldVnode)
753764
}
754765

755766
// replacing existing element
767+
// 挂载节点
756768
const oldElm = oldVnode.elm
769+
// 挂载节点的父节点,例子就是body
757770
const parentElm = nodeOps.parentNode(oldElm)
758771

759772
// create new node

vue/src/platforms/web/runtime/patch.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ import platformModules from 'web/runtime/modules/index'
1010
const modules = platformModules.concat(baseModules)
1111

1212
// 柯里化技巧
13+
/**
14+
* @param {Object} - nodeOps 封装了一系列 DOM 操作的方法
15+
* @param {Object} - modules 定义了一些模块的钩子函数的实现
16+
*/
1317
export const patch: Function = createPatchFunction({ nodeOps, modules })

0 commit comments

Comments
 (0)