Skip to content

Commit

Permalink
Vue初始化过程分析
Browse files Browse the repository at this point in the history
  • Loading branch information
yzsunlei committed Dec 17, 2019
1 parent 7c57d15 commit 9051886
Showing 1 changed file with 5 additions and 158 deletions.
163 changes: 5 additions & 158 deletions 1.2.Vue初始化过程分析-new Vue()到底做了什么.md
Original file line number Diff line number Diff line change
@@ -1,161 +1,8 @@
## 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> };
## 相关
* [https://blog.csdn.net/a419419/article/details/90764860](https://blog.csdn.net/a419419/article/details/90764860)

// 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;
* [https://www.jianshu.com/p/4aa74c1fe182](https://www.jianshu.com/p/4aa74c1fe182)

// private
async: boolean;
* [https://segmentfault.com/a/1190000015371266](https://segmentfault.com/a/1190000015371266)

// 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)
}
}
}
}
```

## Proxy
```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)
* [https://www.cnblogs.com/lalalagq/p/9959560.html](https://www.cnblogs.com/lalalagq/p/9959560.html)

0 comments on commit 9051886

Please sign in to comment.