Skip to content

Instantly share code, notes, and snippets.

@Raynos
Forked from Gozala/weak-map.js
Created November 7, 2011 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/1344853 to your computer and use it in GitHub Desktop.
Save Raynos/1344853 to your computer and use it in GitHub Desktop.
Namespacing shim for ES5
if (typeof(Name) === 'undefined') {
(function(global) {
"use strict";
function defineNamespace(object, namespace) {
var privates = Object.create(object),
base = object.valueOf;
Object.defineProperty(object, 'valueOf', {
value: function valueOf(value) {
if (value !== namespace || this !== object) {
return base.apply(this, arguments);
} else {
return privates;
}
});
return privates;
}
function Name() {
var namespace = {};
return function name(object) {
var privates = object.valueOf(namespace);
return privates !== object ? privates : defineNamespace(object, namespace);
};
}
return global.Name = Name;
})(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment