define
Create a new global environment variable.
globals.define(key, value[, cache])
Defines a new global called key
, who's value defaults to value
.
The following example defines the global
key's default value to the window
object:
globals.define('global', window);
globals.getKeyValue('window') //-> window
If a function is provided and cache
is falsy, that function is run every time the key value is read:
globals.define('isBrowserWindow', function() {
console.log('EVALUATING')
return typeof window !== 'undefined' &&
typeof document !== 'undefined' && typeof SimpleDOM === 'undefined'
}, false);
globals.get('isBrowserWindow') // logs 'EVALUATING'
// -> true
globals.get('isBrowserWindow') // logs 'EVALUATING' again
// -> true
If a function is provided and cache
is truthy, that function is run only the first time the value is read:
globals.define('isWebkit', function() {
console.log('EVALUATING')
var div = document.createElement('div')
return 'WebkitTransition' in div.style
})
globals.getKeyValue('isWebkit') // logs 'EVALUATING'
// -> true
globals.getKeyValue('isWebkit') // Does NOT log again!
// -> true
Parameters
- key
{String}
:The key value to create.
- value
{*}
:The default value. If this is a function, its return value will be used.
- cache
{Boolean}
:Enable cache. If false the
value
function is run every time the key value is read.