Mobx属性作为函数而不是值返回

时间:2017-03-24 09:33:25

标签: javascript mobx

全名getter作为函数而不是value返回。

var person = mobx.observable({
    firstName: 'Matt',
    lastName: 'Ruby',
    age: 0,
    fullname: function() {
     return this.firstName + ' ' + this.lastName
    }
});

console.log(person.fullname) // returns a function instead "Matt Ruby"

Fiddle here

1 个答案:

答案 0 :(得分:0)

已删除自动推理。您需要明确tag the function as a computed

var person = mobx.observable({
    firstName: 'Matt',
    lastName: 'Ruby',
    age: 0,
    fullname: mobx.computed(function() {
        return this.firstName + ' ' + this.lastName;
    })
});

或者:

var person = mobx.observable({
    firstName: 'Matt',
    lastName: 'Ruby',
    age: 0,
    get fullname() {
        return this.firstName + ' ' + this.lastName;
    }
});