BoolUnit
BoolUnit vs boolean
boolean// initialization
let bool = false; // a variable with prmitive boolean value
const unit = new BoolUnit({initialValue: false});
// initialValue is optional, BoolUnit has false as default value
bool === unit // false
bool === unit.value() // true
unit + '' // 'false'
unit + 1 // 1, because false is converted to 0
unit.dispatch(true);
unit + 1 // 2, because true is converted to 1
typeof bool === 'boolean' // true
typeof unit === 'object' // true
typeof unit.value() === 'boolean' // true
// value assignment
bool = true;
unit.dispatch(true);
// value access
console.log(bool) // logs true
console.log(unit.value()) // logs true
// or
unit.subscribe(value => console.log(value)) // logs true, will log future valuesLast updated
Was this helpful?