关于mobx中两种定义observable map的方式的差别

@ckinmind 2017-03-31 03:00:13发表于 ckinmind/ReactCollect mobx

代码如下:

const { observable, toJS, autorun, isObservable, isObservableMap  } = mobx;

class Store {  
  @observable map1 = observable.map();
  @observable map2 = new Map();
}

const store = new Store();

console.log("isObservable");
console.log(isObservable(store.map1));  // true
console.log(isObservable(store.map2)); // true

console.log("isObservableMap");
console.log(isObservableMap(store.map1)); // true
console.log(isObservableMap(store.map2)); // true

store.map1.set("key", []);
store.map2.set("key", []);

console.log("is 'key' observable");
console.log(isObservable(store.map1.get("key"))); // true
console.log(isObservable(store.map2.get("key"))); // false

结论:
使用@observable map1 = observable.map();定义的map类型,不仅本身是observable的,其子元素也是observable的

参考资料:jsfiddle