我们来看看computed的实现。最简单的一个demo如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="app">
<div name="test">{{computeA}}</div> </div>
</body>
<script src="vue.js"></script>
<script type="text/javascript">
new Vue({
el: '#app',
data: function () {
return {
firstName: 111,
lastName: 222
}
},
computed: {
computeA: function () {
return this.firstName + ' ' + this.lastName
}
},
created(){
setTimeout(
() => {
this.firstName = 333;
},1000
)
}
})
</script>
</html>

  

1在初始化实例创建响应式的时候。对options中的computed做了特殊处理:

function initComputed (vm, computed) {
var watchers = vm._computedWatchers = Object.create(null); for (var key in computed) {
var userDef = computed[key];
var getter = typeof userDef === 'function' ? userDef : userDef.get;
{
if (getter === undefined) {
warn(
("No getter function has been defined for computed property \"" + key + "\"."),
vm
);
getter = noop;
}
}
// create internal watcher for the computed property.
watchers[key] = new Watcher(vm, getter, noop, computedWatcherOptions);//为每一个computed项目订制一个watcher // component-defined computed properties are already defined on the
// component prototype. We only need to define computed properties defined
// at instantiation here.
if (!(key in vm)) {
defineComputed(vm, key, userDef);
} else {
if (key in vm.$data) {
warn(("The computed property \"" + key + "\" is already defined in data."), vm);
} else if (vm.$options.props && key in vm.$options.props) {
warn(("The computed property \"" + key + "\" is already defined as a prop."), vm);
}
} function defineComputed (target, key, userDef) {
if (typeof userDef === 'function') {
sharedPropertyDefinition.get = createComputedGetter(key);
sharedPropertyDefinition.set = noop;
} else {
sharedPropertyDefinition.get = userDef.get
? userDef.cache !== false
? createComputedGetter(key)
: userDef.get
: noop;
sharedPropertyDefinition.set = userDef.set
? userDef.set
: noop;
}
Object.defineProperty(target, key, sharedPropertyDefinition);
} function createComputedGetter (key) {//构造该computed的get函数
return function computedGetter () {
var watcher = this._computedWatchers && this._computedWatchers[key];
if (watcher) {
if (watcher.dirty) {
watcher.evaluate();//收集该watcher的订阅
}
if (Dep.target) {
watcher.depend();//同一为这一组订阅再加上组件re-render的订阅(该订阅负责更新组件)
}
return watcher.value
}
}
}

  组件初始化的时候。computed项和data中的分别建立响应式。data中的数据直接对属性的get,set做数据拦截。而computed则建立一个新的watcher,在组件渲染的时候。先touch一下这个computed的getter函数。将这个watcher订阅起来。这里相当于这个computed的watcher订阅了firstname和lastname。touch完后。Dep.target此时又变为之前那个用于更新组件的。再通过watcher.depend()将这个组统一加上这个订阅。这样一旦firstname和lastname变了。同时会触发两个订阅更新。其中一个便是更新组件。重新re-render的函数。

最新文章

  1. mysql replace
  2. form表单修改label样式
  3. [置顶]PADS PCB功能使用技巧系列之NO.003- 如何统一修改元件标号字体?
  4. Sql server之sql注入篇
  5. linux内核宏container_of前期准备之gcc扩展关键字typeof
  6. Linux大神必备-文本编辑器
  7. CentOS6.4安装Smokeping节点监控软件
  8. NHibernate分页
  9. 多设备官方教程(6)控制多版本API
  10. class 类(3) 继承
  11. 什么是PCB改板及PCB改板应注意的问题
  12. UILabel的讲解
  13. Collection中的迭代器
  14. PCA原理分析
  15. Django模型之Meta详解
  16. hdu 1205 吃糖果【鸽巢原理】
  17. iOS调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的
  18. python基础系列教程——Python中的编码问题,中文乱码问题
  19. [翻译] Writing Property Editors 编写属性编辑器
  20. javaFX8初探(环境搭建)

热门文章

  1. web跨域
  2. VIM 单词大小写转换
  3. 算法竞赛模板 动态规划之背包DP
  4. Android多线程:深入分析 Handler机制源码(二)
  5. shell变量替换扩展 变量测试
  6. PHP构建一句话木马
  7. c# 通过地址下载流然后保存文件到本地
  8. vue 项目 跳转 页面 不刷新 问题
  9. cocos2D-X 4.0 build
  10. 对AngularJs的简单了解