@AC
Tea, thank you.
I think you misunderstand. Check my code again. The only object that gets stuff added to it is the single object I refer to as 'lib' within the function, or 'my_lib' outside (same object though). It represents a collection of objects which comprise a library - effectively it is my library - and by tacking them onto a single object it prevents cluttering the global namespace.
Though I didn't say it explicitly, the make_lib() function is called once only. It's defined as a function not so it can easily create loads of identical copies of the library, because that's pointless, but so that the clutter of defining many functions can be defined within, and stay within, a single (albeit massive) function. If I didn't do that then I'd be defining the functions in the global namespace which is exactly what I was trying to avoid.
Prototypes aren't relevant here.
Put another way suppose I wanted to add a hundred functions to my app. I could just define them directly:
function add(a, b) { return a + b; }
function mult(p, q) { return p * q; }
function ... <etc, 98 more times>
Now I've added 100 names to the global namespace, and I can say:
var yy = add(3, 4)
which is pretty obvious, but my 100 definitions can clash with other people's work, or I could do this:
function make_lib() {
var lib = new Object();
lib.add = function(a, b) { return a + b; }
lib.mult = function(p, q) { return p * q; }
lib.... = function ... <etc, 98 more times>
return lib
}
I know have one (not 100) new object; that being the single function make_lib, and I instantiate it thus:
var my_lib = make_lib()
and use it thus:
var yy = lib.add(3, 4)
So, my question remains, is this a sufficient solution to David Wilkinson's namespace problem, and if not, where does it fail?