can-globals
Version:
This module provides a dependency injection container. Modules may define a key and specify a default value (which can be static, cached lazy, or dynamic lazy), but other code can set and reset the value as needed. There is also an event system, for alert
31 lines (21 loc) • 1.08 kB
Markdown
methods used to set and get global variables. For example, get the global [`location`](https://developer.mozilla.org/en-US/docs/Web/API/Window/location) object:
```js
import globals from "can-globals";
const LOCATION = globals.getKeyValue( "location" );
```
New keys can be defined with the `define` method, overwritten with the `setKeyValue` method, and reset to their original value with the `reset` method.
All of these methods are demonstrated in the following example
```js
import globals from "can-globals";
globals.define( "foo", "bar" ); // foo === 'bar'
globals.setKeyValue( "foo", "baz" ); // foo === 'baz'
globals.reset( "foo" ); // foo === 'bar'
```