MCPcopy
hub / github.com/ygs-code/vue / mergeOptions

Function mergeOptions

vue.js:2131–2192  ·  view source on GitHub ↗

* Merge two option objects into a new one. * Core utility used in both instantiation and inheritance. * 将两个对象合成一个对象 将父值对象和子值对象合并在一起,并且优先取值子值,如果没有则取子值 * * 用于实例化和继承的核心实用程序。

(parent, //父值
        child, //子值 优选取子值
        vm)

Source from the content-addressed store, hash-verified

2129 * 用于实例化和继承的核心实用程序。
2130 */
2131 function mergeOptions(parent, //父值
2132 child, //子值 优选取子值
2133 vm) {
2134
2135 {
2136 //检验子组件
2137 checkComponents(child);
2138 }
2139
2140 if (typeof child === 'function') {
2141 //如果child 是函数则获取他的参数
2142 child = child.options;
2143 }
2144 //检查 props 数据类型
2145 normalizeProps(child, vm);
2146
2147 // 将数组转化成对象 比如 [1,2,3]转化成
2148 normalizeInject(child, vm);
2149
2150 // * normalizeDirectives获取到指令对象值。循环对象指令的值,如果是函数则把它变成dirs[key] = {bind: def, update: def} 这种形式
2151 normalizeDirectives(child);
2152
2153 //子组件是否有需要合并的对象继承 方式
2154 var extendsFrom = child.extends;
2155
2156 if (extendsFrom) {
2157 //如果有则递归
2158 parent = mergeOptions(parent, extendsFrom, vm);
2159 }
2160
2161 //如果 子组件有mixins 数组 则也递归合并,继承 方式 mixins 必须是数组
2162 if (child.mixins) {
2163 for (var i = 0, l = child.mixins.length; i < l; i++) {
2164 parent = mergeOptions(parent, child.mixins[i], vm);
2165 }
2166 }
2167 var options = {};
2168 var key;
2169 for (key in parent) { //循环合并后的key
2170 mergeField(key);
2171 }
2172 for (key in child) { //循环子组件的
2173 if (!hasOwn(parent, key)) {
2174 mergeField(key);
2175 }
2176 }
2177
2178 //获取到key 去读取strats类的方法
2179 // strats类 有方法 el,propsData,data,provide,watch,props,methods,inject,computed,components,directives,filters 。
2180 // strats类里面的方法都是 合并数据 如果没有子节点childVal,
2181 // 就返回父节点parentVal,如果有子节点childVal就返回子节点childVal。
2182 function mergeField(key) {
2183 //defaultStrat 获取子值还是父组的值
2184 var strat = strats[key] || //
2185 defaultStrat; //* 如果没有子节点就返回父节点,如果有子节点就返回子节点
2186 //获取子值还是父组的值
2187 options[key] = strat(parent[key], child[key], vm, key);
2188 }

Callers 4

initMixinFunction · 0.85
initMixin$1Function · 0.85
initExtendFunction · 0.85

Calls 6

checkComponentsFunction · 0.85
normalizePropsFunction · 0.85
normalizeInjectFunction · 0.85
normalizeDirectivesFunction · 0.85
mergeFieldFunction · 0.85
hasOwnFunction · 0.85

Tested by

no test coverage detected