I have a global namespace where developers attach object singletons, class definitions, constants, etc. Each JS file has its own crafty flavor of namespace tree initialization.
Maybe a module pattern here:
window.namespace = window.namespace || {};
window.namespace.utils || (window.namespace.utils = {});
(function(util){
util.myutility = new MyUtility();
function MyUtility(){
return {};
}
})(window.namespace.utils)
Something else there:
window.namespace = window.namespace || {};
window.namespace.util = window.namespace.util || {};
window.namespace.util.myutility = new (function(){
return {};
})
Resulting in the same code repeated over and over:
if(!window.namespace)
window.namespace = {};
if(!window.namespace.utils)
window.namespace.utils = {};
...
window.namespace = window.namespace || {};
window.namespace.util = window.namespace.util || {};
...
window.namespace || (window.namespace = {});
window.namespace.utils || (window.namespace.utils = {});
...
$.extend(true,window.namespace,{util:{}});
I know some transpilers optimize these automatically, but aside from that, is there a more solid approach?