You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportfunctioninitExtend(Vue: GlobalAPI){/** * 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=0letcid=1/** * Class inheritance */Vue.extend=function(extendOptions: Object): Function{extendOptions=extendOptions||{}constSuper=thisconstSuperId=Super.cidconstcachedCtors=extendOptions._Ctor||(extendOptions._Ctor={})if(cachedCtors[SuperId]){returncachedCtors[SuperId]}constname=extendOptions.name||Super.options.nameif(process.env.NODE_ENV!=='production'&&name){validateComponentName(name)}constSub=functionVueComponent(options){this._init(options)}Sub.prototype=Object.create(Super.prototype)Sub.prototype.constructor=SubSub.cid=cid++Sub.options=mergeOptions(Super.options,extendOptions)Sub['super']=Super// 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.if(Sub.options.props){initProps(Sub)}if(Sub.options.computed){initComputed(Sub)}// allow further extension/mixin/plugin usageSub.extend=Super.extendSub.mixin=Super.mixinSub.use=Super.use// create asset registers, so extended classes// can have their private assets too.ASSET_TYPES.forEach(function(type){Sub[type]=Super[type]})// enable recursive self-lookupif(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.optionsSub.extendOptions=extendOptionsSub.sealedOptions=extend({},Sub.options)// cache constructorcachedCtors[SuperId]=SubreturnSub}}
再下面就是继承部分了,创建 Sub 构造函数,同时把父类的原型赋值到 Sub.prototype 上。除了原型链的继承,还把父类的静态属性和方法也一并继承了下来:
constSub=functionVueComponent(options){this._init(options)}Sub.prototype=Object.create(Super.prototype)Sub.prototype.constructor=SubSub.cid=cid++Sub.options=mergeOptions(Super.options,extendOptions)Sub['super']=Super// 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.if(Sub.options.props){initProps(Sub)}if(Sub.options.computed){initComputed(Sub)}// allow further extension/mixin/plugin usageSub.extend=Super.extendSub.mixin=Super.mixinSub.use=Super.use// create asset registers, so extended classes// can have their private assets too.ASSET_TYPES.forEach(function(type){Sub[type]=Super[type]})// enable recursive self-lookupif(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.optionsSub.extendOptions=extendOptionsSub.sealedOptions=extend({},Sub.options)// cache constructorcachedCtors[SuperId]=SubreturnSub
在上一篇构造函数部分,我们知道了在
global-api
添加全局 API 的时候,在 Vue 上增加了Vue.extend
方法,因为考虑到后面讲解 initMixin 部分有涉及到这部分的内容,所以今天先来讲讲这个Vue.extend
好了。通过 Vue 的 extend 文档 我们知道,它是使用基础 Vue 构造器,可以用来创建一个“子类”。比如:
结果如下:
我们直接来看下代码:
下面我们来逐行分析下代码,
extendOptions
为通过Vue.extend
透传进去的options
对象,Super
指向当前构造函数Vue
。初始给 Vue 添加一个cid
,它的值为0,之后每次通过Vue.extend
创建的子类的cid
值依次递增。extendOptions._Ctor
是用来缓存子类构造函数,如果已存在就直接返回。在讲全局 API 的时候,我们有讲到过
Vue.component
这个方法,它是用来注册子组件的如果当
type = 'component'
且definition
是一个对象时,比如:就会执行
this.options._base.extend(definition)
,相当于是Vue.extend(definition)
,这里的name
就是my-components-name
。所以在Vue.extend
中下面这段代码是用来校验组件名称是否规范用的。再下面就是继承部分了,创建
Sub
构造函数,同时把父类的原型赋值到Sub.prototype
上。除了原型链的继承,还把父类的静态属性和方法也一并继承了下来:最终返回子类。
小结
我们可以看下,现在子类
Sub
上已经包含了哪些属性和方法:对比了上一篇中父类上列举的属性和方法,除了新增部分,
Sub
上没有的属性包括:参考
The text was updated successfully, but these errors were encountered: