// 暴露工具方法 // exposed util methods. // 注意:这不是全局公共API的一部分, // 除非了解到它们会带来的风险否则请避免使用。 // 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、delete、nextTick Vue.set = set Vue.delete = del Vue.nextTick = nextTick
// 这用于标识“基础”构造函数 // 以在Weex的多实例场景中扩展所有普通对象组件 // this is used to identify the "base" constructor to extend all plain-object // components with in Weex's multi-instance scenarios. Vue.options._base = Vue
// 定义并导出initExtend exportfunctioninitExtend (Vue: GlobalAPI) { // 每个实例构造函数,包括Vue都有唯一的cid。 // 这使我们能够为原型继承创建包装的“子构造函数”并缓存它们。 /** * Each instance constructor, including Vue, has a unique * cid. This enables us to create wrapped "child * constructors" for prototypal inheritance and cache them. */ // 设置Vue的cid为0 Vue.cid = 0 // 定义cid变量 let cid = 1
// 定义类继承方法 /** * Class inheritance */ // 定义Vue类静态方法extend,接受扩展选项对象 Vue.extend = function (extendOptions: Object): Function{ // extendOptions若未定义则设置为空对象 extendOptions = extendOptions || {} // 存储父类和父类的cid const Super = this const SuperId = Super.cid // 定义缓存构造器对象,如果扩展选项的_Ctor属性未定义则赋值空对象 const cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {}) // 如果缓存构造器已存有该构造器,则直接返回 if (cachedCtors[SuperId]) { return cachedCtors[SuperId] }
// 获取扩展配置对象名称或父级配置对象名称属性,赋值给name const name = extendOptions.name || Super.options.name // 在非生产环境下验证name是否合法并给出警告 if (process.env.NODE_ENV !== 'production' && name) { validateComponentName(name) }
// 对于props和computed属性,扩展时在Vue实例上定义了代理getter。 // 这避免了对每个创建的实例执行Object.defineProperty调用。 // For props and computed properties, we define the proxy getters on // the Vue instances at extension time, on the extended prototype. This // avoids Object.defineProperty calls for each instance created. // 初始化子类的props if (Sub.options.props) { initProps(Sub) } // 初始化子类的计算属性 if (Sub.options.computed) { initComputed(Sub) }
// 创建子类的资源注册方法,允许子类有私有资源 // create asset registers, so extended classes // can have their private assets too. ASSET_TYPES.forEach(function (type) { Sub[type] = Super[type] }) // 启用递归自查找 // enable recursive self-lookup if (name) { Sub.options.components[name] = Sub }
// 在扩展时保持对父类配置对象的引用, // 以后实例化时可以检查父级配置对象是否更新 // keep a reference to the super options at extension time. // later at instantiation we can check if Super's options have // been updated. Sub.superOptions = Super.options Sub.extendOptions = extendOptions Sub.sealedOptions = extend({}, Sub.options)
// 缓存子类构造函数 // cache constructor cachedCtors[SuperId] = Sub // 返回 return Sub } }