strats对象添加如下3个方法:
strats
components
directives
filters
// Assets function mergeAssets ( parentVal: ?Object, childVal: ?Object, vm?: Component, key: string ): Object { const res = Object.create(parentVal || null) if (childVal) { process.env.NODE_ENV !== 'production' && assertObjectType(key, childVal, vm) return extend(res, childVal) } else { return res } }
分析:
以parentVal为原型创建一个res对象
parentVal
res
如果childVal存在,将childVal对象合并到res对象上,同名key覆盖,不同命赋值,并返回合并后的res对象
childVal
如果childVal不存在,直接返回res对象
遍历ASSET_TYPES,为每个资源添加mergeAssets方法
ASSET_TYPES
mergeAssets
// ASSET_TYPES = ['component','directive','filter'] ASSET_TYPES.forEach(function (type) { strats[type + 's'] = mergeAssets })
assertObjectType函数的作用是检测一直变量是否是纯对象
assertObjectType
function assertObjectType (name: string, value: any, vm: ?Component) { if (!isPlainObject(value)) { warn( `Invalid value for option "${name}": expected an Object, ` + `but got ${toRawType(value)}.`, vm ) } }
Last updated 5 years ago
Was this helpful?