执行import Vue from ‘vue’时代码入口是src/platforms/web/entry-runtime-with-compiler.js
Vue庐山真面目

import { initMixin } from './init'import { stateMixin } from './state'import { renderMixin } from './render'import { eventsMixin } from './events'import { lifecycleMixin } from './lifecycle'import { warn } from '../util/index'function Vue (options) {if (process.env.NODE_ENV !== 'production' &&!(this instanceof Vue)) {warn('Vue is a constructor and should be called with the `new` keyword')}this._init(options)}initMixin(Vue)stateMixin(Vue)eventsMixin(Vue)lifecycleMixin(Vue)renderMixin(Vue)export default Vue
使用Function实现的类,只能通过new Vue去实例它
为何不用ES6的Class去实现呢?
代码后边有 xxxMixin 的函数调用,它们是在传入的参数Vue的prototype上扩展一些方法
Vue按功能把这些扩展分散到多个模块中去实现,而不是在一个模块里实现所有,这种方式使用Class难以实现
这么做的好处是非常方便代码的维护和管理
initGlobalAPI
给Vue对象本身扩展全局静态方法
/* @flow */import config from '../config'import { initUse } from './use'import { initMixin } from './mixin'import { initExtend } from './extend'import { initAssetRegisters } from './assets'import { set, del } from '../observer/index'import { ASSET_TYPES } from 'shared/constants'import builtInComponents from '../components/index'import { observe } from 'core/observer/index'import {warn,extend,nextTick,mergeOptions,defineReactive} from '../util/index'export function initGlobalAPI (Vue: GlobalAPI) {// configconst configDef = {}configDef.get = () => configif (process.env.NODE_ENV !== 'production') {configDef.set = () => {warn('Do not replace the Vue.config object, set individual fields instead.')}}Object.defineProperty(Vue, 'config', configDef)// exposed util methods.// NOTE: these are not considered part of the public API - avoid relying on// them unless you are aware of the risk.Vue.util = {warn,extend,mergeOptions,defineReactive}Vue.set = setVue.delete = delVue.nextTick = nextTick// 2.6 explicit observable APIVue.observable = <T>(obj: T): T => {observe(obj)return obj}Vue.options = Object.create(null)ASSET_TYPES.forEach(type => {Vue.options[type + 's'] = Object.create(null)})// this is used to identify the "base" constructor to extend all plain-object// components with in Weex's multi-instance scenarios.Vue.options._base = Vueextend(Vue.options.components, builtInComponents)initUse(Vue)initMixin(Vue)initExtend(Vue)initAssetRegisters(Vue)}
Vue.utils暴露的方法最好不要依赖,它们经常会发生变化,是不稳定的
