Skip to content

Instantly share code, notes, and snippets.

@ixtli
Created July 29, 2014 21:58
Show Gist options
  • Save ixtli/90e0085438010ea0f9e1 to your computer and use it in GitHub Desktop.
Save ixtli/90e0085438010ea0f9e1 to your computer and use it in GitHub Desktop.
A thing that acts as a no-conflict stack for those times when you have to mutate the global namespace.
'use strict';
define(function(require)
{
var UNDEFINED = void 0;
function NoConflict(namespace)
{
this._namespace = namespace ? namespace : window;
this._stack = [];
}
NoConflict.prototype.push = function(key, value)
{
var defined = this._namespace.hasOwnProperty(key);
var state = {'key': key, 'value': this._namespace[key], 'defined': defined};
this._stack.push(state);
this._namespace[key] = value;
};
NoConflict.prototype.pop = function()
{
var old = this._stack.pop();
if (!old)
{
return null;
}
if (old.defined)
{
this._namespace[old.key] = old.value;
} else {
delete this._namespace[old.key];
}
return old;
};
NoConflict.prototype.empty = function()
{
while (this.pop());
};
return NoConflict;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment