(vm, computed)
| 5038 | |
| 5039 | //初始化计算属性 并且判断属性的key 是否 在 data ,将 计算属性的key 添加入监听者中 |
| 5040 | function initComputed(vm, computed) { |
| 5041 | // $flow-disable-line |
| 5042 | //创建一个新的监听者对象空对象 |
| 5043 | var watchers = vm._computedWatchers = Object.create(null); |
| 5044 | // computed properties are just getters during SSR 计算的属性只是SSR期间的getter |
| 5045 | var isSSR = isServerRendering(); // 服务器呈现 判断是不是node 服务器环境 |
| 5046 | |
| 5047 | for (var key in computed) { |
| 5048 | var userDef = computed[key]; //获取值 |
| 5049 | var getter = typeof userDef === 'function' ? userDef : userDef.get; //获取值函数 |
| 5050 | if ("development" !== 'production' && getter == null) { //如果getter 是 空 警告 |
| 5051 | warn( |
| 5052 | ("Getter is missing for computed property \"" + key + "\"."), |
| 5053 | vm |
| 5054 | ); |
| 5055 | } |
| 5056 | |
| 5057 | if (!isSSR) { //如果不是node ssr渲染 |
| 5058 | // create internal watcher for the computed property. |
| 5059 | watchers[key] = new Watcher( |
| 5060 | vm, //vm vode |
| 5061 | getter || noop, //函数 |
| 5062 | noop, //回调函数 |
| 5063 | computedWatcherOptions //参数 lazy = true |
| 5064 | ); |
| 5065 | } |
| 5066 | |
| 5067 | // component-defined computed properties are already defined on the 组件定义的计算属性已经在 |
| 5068 | // component prototype. We only need to define computed properties defined 组件原型。我们只需要定义已定义的计算属性 |
| 5069 | // at instantiation here. 在实例化。 |
| 5070 | if (!(key in vm)) { //如果computed 属性key 不在虚拟dom中 |
| 5071 | defineComputed(vm, key, userDef); //定义计算属性 并且 把属性的数据 添加到对象监听中 |
| 5072 | } else { |
| 5073 | if (key in vm.$data) { //如果判断属性监听的key在 data 中则发出警告 |
| 5074 | warn(("The computed property \"" + key + "\" is already defined in data."), vm); |
| 5075 | } else if (vm.$options.props && key in vm.$options.props) { |
| 5076 | warn(("The computed property \"" + key + "\" is already defined as a prop."), vm); |
| 5077 | } |
| 5078 | } |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | //定义计算属性 并且 把属性的数据 添加到对象监听中 |
| 5083 | function defineComputed(target, //目标 |
no test coverage detected