For the code below, I want to use module pattern to keep the members private and access them through a getter. It is so simple there is no need to put any initialization in the constructor. Is this O.K?
/**
*Global - in process of removing
*/
var Globals = ( function ()
{
var globals =
{
tag_array: [],
load_on: 0,
current_tag: 0,
TIME: 4000,
PASS: 0,
FAIL: 1,
NOTDEFINED: 2
};
var GlobalsInternal = function ( )
{
};
GlobalsInternal.prototype.get = function( type )
{
return globals[ type ];
};
return GlobalsInternal;
} () );
Use
new Globals().get( 'TIME' );
JavaScript doesn’t have classes.
So no constructor-function means no constructor-function and an empty constructor-function is still a constructor-function.
Thus, if
new Xis desired, thenXmust evaluate to a constructor-function (which is really just a function that knows how to behave in context).In any case, that’s “okay”. Just keep in mind that there is only one object named by
globals(and it is closed over in thegetfunction with such a name).(I think josh3736’s answer has merit for just a “global dump”. A globally-named object in JS is a singleton in other OO-language senses.)