ensure-key
Version:
Safe member access with initial value assignment.
19 lines (11 loc) • 505 B
Markdown
*Lets you access members of an object safely (will be added if missing and initialized to a value you provide).*
**How to use:**
```
import './Ensure';
const registry = {};
registry.ensure('abc', {}).ensure('items', []).push(123);
console.log(registry); // --> { abc: { items: [ 123 ] } }
```
The ensure method is added to the Object prototype and is thus available on every object.
It is hidden from enumeration (to not cause interferences with environments).