计算属性 Computed
基于 ES6 语法,在对象上面增加或减少属性,也就是计算属性。
ES6
let obj = {
name: 'Aziz',
age: '18'
}
Object.defineProperty(obj, 'all', {
get() {
console.log(this.name + ',' + this.age)
},
set(youName) {
this.name = youName
}
})
console.log(obj.all)
// Aziz,18
obj.all = '小明'
console.log(obj.all)
// 小明,18
Vue
new Vue({
template: `
<div id="app">{{ fullName }}</div>
`,
el: '#app',
data: {
firstName: 'Foo',
lastName: 'Bar'
},
computed: {
fullName: function () {
return this.firstName + ' ' + this.lastName
}
}
})