Created
July 29, 2014 21:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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