版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20042935/article/details/89318711
计算属性是监控属性的强化版,它必须依赖于 1个或多个监控属性。通过普通的监控属性实现对视图的监听,它自身的变化也由监控属性进行驱动。
计算属性集中定义在$computed对象中。有多种形式。
//函数形式的只读计算属性
avalon.define({
$id: 'test',
firstName: '333',
lastName: 'xxx',
$computed: {
//fullName依赖于firstName与lastName
fullName: function(){
return this.firstName+' '+this.lastName
},
//xxx只依赖于firstNaem
xxx: function(){
return this.firstName+'!!'
}
}
})
//对象形式的可读写计算属性
avalon.define({
$id: 'test',
firstName: '333',
lastName: 'xxx',
$computed: {
//fullName依赖于firstName与lastName
fullName: {
get: function(){
return this.firstName+' '+this.lastName
},
set: function(val){
var arr = val.split(' ')
this.firstName = arr[0]
this.lastName = arr[1]
}
}
}
})