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) 的別名