mobx动态添加observable
mobx使用extendObservable来动态添加observable属性。
extendObservable(target, properties, decorators?, options?)
例如:
var Person = function(firstName, lastName) { // 在一个新实例上初始化 observable 属性 extendObservable(this, { firstName: firstName, lastName: lastName, get fullName() { return this.firstName + " " + this.lastName }, setFirstName(firstName) { this.firstName = firstName } }, { setFirstName: action }); } var matthew = new Person("Matthew", "Henry"); // 向 observable 对象添加 observable 属性 extendObservable(matthew, { age: 353 });
observable.object(object) 实际上是 extendObservable({}, object) 的别名