mergeOptions看完之后,回到initMixin来,注意到在mergeOptions函数中的parent参数是一个resolveConstructorOptions函数的返回值,vue
vm.$options = mergeOptions(
resolveConstructorOptions(vm.constructor),
options || {},
vm
);
//主体
function resolveConstructorOptions (Ctor) {
var options = Ctor.options;
if (Ctor.super) {
var superOptions = resolveConstructorOptions(Ctor.super);
var cachedSuperOptions = Ctor.superOptions;
if (superOptions !== cachedSuperOptions) {
// super option changed,
// need to resolve new options.
Ctor.superOptions = superOptions;
// check if there are any late-modified/attached options (#4976)
var modifiedOptions = resolveModifiedOptions(Ctor);
// update base extend options
if (modifiedOptions) {
extend(Ctor.extendOptions, modifiedOptions);
}
options = Ctor.options = mergeOptions(superOptions, Ctor.extendOptions);
if (options.name) {
options.components[options.name] = Ctor;
}
}
}
return options
}
复制代码
以上是函数主体,先是建立了一个options对象来保存当前ctor的iotionps,若是该ctor没有super属性就直接返回options,那么这个super是个什么东西呢,找找资料:segmentfault
Ctor.super是经过Vue.extend构造子类的时候。Vue.extend方法会为Ctor添加一个super属性,指向其父类构造器函数
以上来源:https://segmentfault.com/a/1190000014587126this
这时候仍是去看看extend 函数把:spa
Vue.extend = function (extendOptions) {
extendOptions = extendOptions || {};//若是没有传入options则赋值为空对象
var Super = this;//指向vue构造器
var SuperId = Super.cid;//vue的id? 啥意思啊
var cachedCtors = extendOptions._Ctor || (extendOptions._Ctor = {});//是否已注册,有没有父组件,没有则为空
if (cachedCtors[SuperId]) {//若是有构造器组件,则直接返回构造器组件的id
return cachedCtors[SuperId]
}
var name = extendOptions.name || Super.options.name;//建立name
if ("development" !== 'production' && name) {//跳过先
validateComponentName(name);
}
var Sub = function VueComponent (options) {//建立Sub对象
this._init(options);
};
Sub.prototype = Object.create(Super.prototype);//从新函数指向原型
Sub.prototype.constructor = Sub;//
Sub.cid = cid++;//给sub添加id
Sub.options = mergeOptions(//合并options
Super.options,
extendOptions
);
Sub['super'] = Super;//给sub添加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.
//对于props和computed属性,咱们在
//扩展时扩展原型上的Vue实例。这个
//避免为每一个建立的实例调用object.defineproperty。
if (Sub.options.props) {//在sub的原型上建立_props属性,而且按照props建立访问器属性添加到该属性上(为何不直接放在sub上呢?)
initProps$1(Sub);
}
if (Sub.options.computed) {//建立计算器属性,具体同上
initComputed$1(Sub);
}
// allow further extension/mixin/plugin usage
Sub.extend = Super.extend;//复制构造器的extent属性
Sub.mixin = Super.mixin;//复制构造器的mixin属性
Sub.use = Super.use;//复制构造器的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-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.
//保持和父组件options的扩展性更新,即便extend已经被实例化
Sub.superOptions = Super.options;//父级options
Sub.extendOptions = extendOptions;//本身的options
Sub.sealedOptions = extend({}, Sub.options);//合并的options
// cache constructor
cachedCtors[SuperId] = Sub;//建立superId属性指向sub构造器
return Sub
};
复制代码
整整看了半天,得出:Ctor.superOptions=父级组件的optionsprototype
那resolveConstructorOptions函数的目的也就比较好理解了:主要是解构获取构造器的options(函数名也能看出来),主要是其中有对于若是构造器也是extend添加的时候应该怎么处理,以及它们的构造器若是有更新扩展,须要及时更新到下级。code
function resolveConstructorOptions (Ctor) {
var options = Ctor.options;
if (Ctor.super) {//是否有父级组件
var superOptions = resolveConstructorOptions(Ctor.super);//递归解构父级组件,获取全部上级的options合集
var cachedSuperOptions = Ctor.superOptions;//父级组件的options
if (superOptions !== cachedSuperOptions) {//若是父级组件被改变过,更新superOptions
// super option changed,
// need to resolve new options.
Ctor.superOptions = superOptions;
// check if there are any late-modified/attached options (#4976)
//检查是否有任何后期修改/附加选项
var modifiedOptions = resolveModifiedOptions(Ctor);
// update base extend options
//更新扩展的options
if (modifiedOptions) {
extend(Ctor.extendOptions, modifiedOptions);
}
options = Ctor.options = mergeOptions(superOptions, Ctor.extendOptions);//合并options
if (options.name) {
options.components[options.name] = Ctor;
}
}
}
return options
}复制代码