Created
May 9, 2012 21:50
-
-
Save keriati/2649181 to your computer and use it in GitHub Desktop.
Just some 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
(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