Skip to content

Instantly share code, notes, and snippets.

@keriati
Created May 9, 2012 21:50
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 keriati/2649181 to your computer and use it in GitHub Desktop.
Save keriati/2649181 to your computer and use it in GitHub Desktop.
Just some namespace
(function() {
function myNS() {
var myNS = this;
function _copy(target, source) {
var key;
for (key in source) {
if (typeof target[key] === 'undefined') {
target[key] = source[key];
} else {
myNS.log('ns already taken');
}
}
return target;
}
function _create(name) {
var node = window.myNS,
parts = name ? name.split('.') : [],
pl = parts.length,
i;
for (i = 0; i < pl; i++) {
var part = parts[i];
var nso = node[part];
if (!nso) {
nso = {};
node[part] = nso;
}
node = nso;
}
return node;
}
this.log = function(string) {
if(window.console) {
window.console.log(string);
}
};
this.extend = function(target, source) {
return _copy(
_create(target),
source
);
}
}
window.myNS = window.myNS || new myNS();
})();
(function() {
myNS.extend('tools', {
tooloptions: function() {
console.log('tooloptions');
},
tooloptions2: function() {
console.log('tooloptions2');
}
});
myNS.extend('tools.subtools', {
subtooloptions: function() {
console.log('subtooloptions');
},
subtooloptions2: function() {
console.log('subtooloptions2');
}
});
myNS.extend('othertools.subtools', {
othersubtooloptions: function() {
console.log('othersubtooloptions');
},
othersubtooloptions2: function() {
console.log('othersubtooloptions2');
}
});
myNS.extend('othertools.subtools', {
othersubtooloptions: function() {
console.log('overwrite here');
},
othersubtooloptions2: function() {
console.log('overwrite here');
}
});
console.log(myNS);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment