How to check if object is Immutable?

后端 未结 6 796
有刺的猬
有刺的猬 2020-12-29 01:44

Immutable object can be an instance of:

  • Immutable.List
  • Immutable.Map
  • Immutable.OrderedMap
6条回答
  •  北海茫月
    2020-12-29 02:16

    And this way you can get to know what type of Immutable Iterable variable is:

    const obj0 = 'xxx';
    const obj1 = Immutable.fromJS({x: 'XXX', z: 'ZZZ'});
    const obj2 = Immutable.fromJS([ {x: 'XXX'}, {z: 'ZZZ'}]);
    
    const types = ['List', 'Stack', 'Map', 'OrderedMap', 'Set', 'OrderedSet'];
    const type0 = types.find(currType => Immutable[currType][`is${currType}`](obj0));
    const type1 = types.find(currType => Immutable[currType][`is${currType}`](obj1));
    const type2 = types.find(currType => Immutable[currType][`is${currType}`](obj2));
    
    console.log(`Obj0 is: ${type0}`); // Obj0 is: undefined
    console.log(`Obj1 is: ${type1}`); // Obj1 is: Map
    console.log(`Obj2 is: ${type2}`); // Obj2 is: List

提交回复
热议问题