When I try and make different instances of this module, it does not work.
It seems to be a singleton. I can only have one instance at a time.
What mechanism limits the constructor function publik() to only have on instance?
var Module = ( function ()
var publik = function ( )
publik.prototype.get = function()
document.getElementById( 'a'+test ).innerHTML = test;
publik.prototype.set = function( value )
test = value;
} ) ();
var object1 = new Module();
var object2 = new Module();
The short answer: closure.
The long answer (if I have it right, please comment so I can correct):
Your Module var is a executed immediately when the script loads. (denoted by the parenthesis around the function.)()
In that module, your publik var is declared and it's left in the closure even when the function completes!
With subsequent calls, you still access that one Module that was auto-executed. And it always gets that same closure space and function scope and the same object, in short - so your publik variable is actually always the same one.