DictUnit
DictUnit vs object literal vs Map:
object literal vs Map:// initialization
const obj = {a: 1}; // an object literal
const map = new Map([['a', 1]]); // a map with a key:value pair 'a':1
const unit = new DictUnit({initialValue: {a: 1}}); // value is {a: 1}
obj === map // false
obj === unit // false
map === unit // false
typeof obj === 'object' // true
typeof map === 'object' // true
typeof unit === 'object' // true
unit instanceof Map // false
// adding a property
obj.b = 2;
map.set('b') = 2;
unit.set('b') = 2; // this is reactive, creates and dispatches a new object
map.set(obj, unit) // works
map.get(obj) // returns unit
unit.set(obj, map) // won't work, the key has to be a number or string
// additionally, non serialzable values are not recommended,
// when you configure the Unit to be immutable,
// the Unit will create a clone of the value but the Map will not be cloned.
// And when you configure the Unit to be persistent,
// the Unit will save it to localStorage using JSON.stringify but Map doesn't
// work with JSON.stringify, it simply becomes "{}".
map.clear() // clears all the key-value pairs
unit.clearValue() // clears the value and emits the default value {},
// this is similar to map.clear()
unit.clear() // clears the value and emits the default value ie: empty {},
// it also clears the cache, and unfreezes the unitLast updated
Was this helpful?