-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
735 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
## Vue源码学习前置知识 | ||
* 以下是我整理的Vue源码学习前置知识:Flow、DefineProperty、Vnode、类继承、递归、代理。可以针对这些点先好好理解一下。 | ||
|
||
## Flow | ||
|
||
```vuejs | ||
// 摘自源码中src/core/config.js | ||
export type Config = { | ||
// user | ||
optionMergeStrategies: { [key: string]: Function }; | ||
silent: boolean; | ||
productionTip: boolean; | ||
performance: boolean; | ||
devtools: boolean; | ||
errorHandler: ?(err: Error, vm: Component, info: string) => void; | ||
warnHandler: ?(msg: string, vm: Component, trace: string) => void; | ||
ignoredElements: Array<string | RegExp>; | ||
keyCodes: { [key: string]: number | Array<number> }; | ||
// platform | ||
isReservedTag: (x?: string) => boolean; | ||
isReservedAttr: (x?: string) => boolean; | ||
parsePlatformTagName: (x: string) => string; | ||
isUnknownElement: (x?: string) => boolean; | ||
getTagNamespace: (x?: string) => string | void; | ||
mustUseProp: (tag: string, type: ?string, name: string) => boolean; | ||
// private | ||
async: boolean; | ||
// legacy | ||
_lifecycleHooks: Array<string>; | ||
}; | ||
``` | ||
|
||
## DefineProperty | ||
|
||
```vuejs | ||
// 摘自源码中src/core/instance/state.js | ||
Object.defineProperty(Vue.prototype, '$data', dataDef) | ||
Object.defineProperty(Vue.prototype, '$props', propsDef) | ||
// 摘自源码中src/core/observer/index.js | ||
Object.defineProperty(obj, key, { | ||
enumerable: true, | ||
configurable: true, | ||
get: function reactiveGetter () { | ||
const value = getter ? getter.call(obj) : val | ||
if (Dep.target) { | ||
dep.depend() | ||
if (childOb) { | ||
childOb.dep.depend() | ||
if (Array.isArray(value)) { | ||
dependArray(value) | ||
} | ||
} | ||
} | ||
return value | ||
}, | ||
set: function reactiveSetter (newVal) { | ||
const value = getter ? getter.call(obj) : val | ||
/* eslint-disable no-self-compare */ | ||
if (newVal === value || (newVal !== newVal && value !== value)) { | ||
return | ||
} | ||
/* eslint-enable no-self-compare */ | ||
if (process.env.NODE_ENV !== 'production' && customSetter) { | ||
customSetter() | ||
} | ||
// #7981: for accessor properties without setter | ||
if (getter && !setter) return | ||
if (setter) { | ||
setter.call(obj, newVal) | ||
} else { | ||
val = newVal | ||
} | ||
childOb = !shallow && observe(newVal) | ||
dep.notify() | ||
} | ||
}) | ||
``` | ||
|
||
## Vnode | ||
|
||
```vuejs | ||
// 摘自源码中src/core/vdom/vnode.js | ||
export default class VNode { | ||
tag: string | void; | ||
data: VNodeData | void; | ||
children: ?Array<VNode>; | ||
text: string | void; | ||
elm: Node | void; | ||
ns: string | void; | ||
context: Component | void; // rendered in this component's scope | ||
key: string | number | void; | ||
componentOptions: VNodeComponentOptions | void; | ||
componentInstance: Component | void; // component instance | ||
parent: VNode | void; // component placeholder node | ||
// 省略 | ||
} | ||
``` | ||
|
||
## 类继承 | ||
|
||
```vuejs | ||
// 摘自源码中src/core/global-api/extend.js | ||
Vue.extend = function (extendOptions: Object): Function { | ||
extendOptions = extendOptions || {} | ||
const Super = this | ||
const SuperId = Super.cid | ||
// 省略 | ||
Sub.prototype = Object.create(Super.prototype) | ||
Sub.prototype.constructor = Sub | ||
Sub.cid = cid++ | ||
Sub.options = mergeOptions( | ||
Super.options, | ||
extendOptions | ||
) | ||
Sub['super'] = Super | ||
// 省略 | ||
} | ||
``` | ||
|
||
## 递归 | ||
|
||
```vuejs | ||
// 摘自源码中src/core/vdom/create-element.js | ||
function applyNS (vnode, ns, force) { | ||
vnode.ns = ns | ||
if (vnode.tag === 'foreignObject') { | ||
// use default namespace inside foreignObject | ||
ns = undefined | ||
force = true | ||
} | ||
if (isDef(vnode.children)) { | ||
for (let i = 0, l = vnode.children.length; i < l; i++) { | ||
const child = vnode.children[i] | ||
if (isDef(child.tag) && ( | ||
isUndef(child.ns) || (isTrue(force) && child.tag !== 'svg'))) { | ||
applyNS(child, ns, force) | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## 代理 | ||
|
||
```vuejs | ||
// 摘自源码中src/core/instance/proxy.js | ||
initProxy = function initProxy (vm) { | ||
if (hasProxy) { | ||
// determine which proxy handler to use | ||
const options = vm.$options | ||
const handlers = options.render && options.render._withStripped | ||
? getHandler | ||
: hasHandler | ||
vm._renderProxy = new Proxy(vm, handlers) | ||
} else { | ||
vm._renderProxy = vm | ||
} | ||
} | ||
``` | ||
|
||
## 相关 | ||
* [https://juejin.im/post/5b4ad441f265da0f7d4eeb7a](https://juejin.im/post/5b4ad441f265da0f7d4eeb7a) | ||
* [https://www.jianshu.com/p/c914ccd498e7](https://www.jianshu.com/p/c914ccd498e7) | ||
* [https://blog.csdn.net/qq_24122593/article/details/53197288](https://blog.csdn.net/qq_24122593/article/details/53197288) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.