BoolUnit
BoolUnit is a type of Unit that only accepts boolean data type as its value. It ensures that at any point of time the value would always be boolean.
It implements non-deprecated Object.prototype methods like toString and redirects them to the stored boolean value inside the Unit, so when you call BoolUnit.toString() , it'll be executed on the Unit's value instead of the BoolUnit instance itself. Similarly, when you call other Object.prototype methods they are called on the stored value instead of BoolUnit instance.
See API reference for more details.
​
​
Default value
false
Value data type
boolean

BoolUnit vs boolean

BoolUnit can not be used as a drop-in replacement for primitive boolean value, since BoolUnit is a non-primitive data structure, and the actual boolean value is stored inside the Unit. See the below comparisons for more clarity.
1
// initialization
2
let bool = false; // a variable with prmitive boolean value
3
const unit = new BoolUnit({initialValue: false});
4
// initialValue is optional, BoolUnit has false as default value
5
​
6
bool === unit // false
7
bool === unit.value() // true
8
​
9
unit + '' // 'false'
10
unit + 1 // 1, because false is converted to 0
11
unit.dispatch(true);
12
unit + 1 // 2, because true is converted to 1
13
​
14
typeof bool === 'boolean' // true
15
typeof unit === 'object' // true
16
typeof unit.value() === 'boolean' // true
17
​
18
// value assignment
19
bool = true;
20
unit.dispatch(true);
21
​
22
// value access
23
console.log(bool) // logs true
24
console.log(unit.value()) // logs true
25
// or
26
unit.subscribe(value => console.log(value)) // logs true, will log future values
Copied!
Last modified 10mo ago
Copy link