Skip to content

Instantly share code, notes, and snippets.

@azu
Forked from mattn/gist:838338
Created February 22, 2011 07:44
Show Gist options
  • Save azu/838345 to your computer and use it in GitHub Desktop.
Save azu/838345 to your computer and use it in GitHub Desktop.
(function(ns) {
var r = typeof window != 'undefined' ? window : this;
[].forEach.call(ns.split("×"), function(v) { r = r[v] = {} })
})("mattn×hasegawayosuke×tokuhirom")
mattn.hasegawayosuke.tokuhirom.Miyagawa = function() {
alert('×××')
}
mattn.hasegawayosuke.tokuhirom.Miyagawa()
// Another
var namespace = function (ns_string) {
var parts = ns_string.split('.'),
parent = typeof window != 'undefined' ? window : this,
i;
// strip redundant leading global
if (parts[0] === parent.toString()) {
parts = parts.slice(1);
}
for (i = 0; i < parts.length; i += 1) {
// create a property if it doesn't exist
if (typeof parent[parts[i]] === "undefined") {
parent[parts[i]] = {};
}
parent = parent[parts[i]];
}
return parent;
};
namespace("mattn.hasegawayosuke.tokuhirom");
mattn.hasegawayosuke.tokuhirom.Miyagawa = function() {
alert('ouffu')
}
mattn.hasegawayosuke.tokuhirom.Miyagawa()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment