I’m about to start building a JS library that will have multiple modules. Let’s suppose the library is called Library, and two modules will be called One and Two. I’d like for end users to be able to call the library in two different ways:
Basically, I want to give the end users the option of including a namespace or not. Is there a good way to do this? Also, is there a good way to do this if I also want to provide a minified version of the library? This library is something that I could end up in Node.js; for now, I’m going to use it myself, but I want to design it in such a way that it’s not too hard to turn in to a sharable project in the future.
Any references you can point me to would be great, thanks!
Problem courtesy of: anon
well, i don’t know what you mean by “good way”.
First of all, the whole purpose of a namespace is to collect variables that are related and not scatter them all around your public namespace. Personally I wouldn’t use such a thing, but you could loop through your namespace’s objects and attach them to the window :
for(var i in Namespace)
window[i] = Namespace[i];
Solution courtesy of: gion_13