javascript - Dojo Singleton or at least static method/variable? -
does know how make dojo class singleton, or @ least how create static method or variable in dojo class?
i achieve having global variable each class , method sets variable if null, crappy solution. having singleton class nicer because 1 inherit , voilá has singleton :)
heinrich
if use dojo.declareclass
create new class, can use new
operator create new instance of it. in java, singleton implemented using private constructor, javascript doesn't have kind of capability. can not create java-like singleton in javascript.
so typical way create singleton :
if (typeof dojo.getobject('x.y.z') === 'undefined') { dojo.setobject('x.y.z', { //object definitions }); }
to create static variable, add variable dojo class object.
dojo.declare("x.y.abc", {}); x.y.abc.globalv = 'abc';
when used dojo.require
, javascript file won't loaded twice dojo, don't need check existence of variable.
Comments
Post a Comment