Single-mode notes
Also known as the singleton mode, only allows the object class to be instantiated once
Usage:
1. Namespaces: Use an object to plan a namespace, organize properties and methods on managed objects
2. Static variable management: Let the created function execute once, save the static variable within the created object through the accessor, and place the object as a singleton in global space as a static variable singleton object for others to use
3. Lazy Singleton: Sometimes it takes a singleton to create a delay, and this form of deferred creation is called "Lazy creation".
Code:
1 //name Space2 varJt = {3 //managing static Variables4Conf: (function(){5 varconf = {6max_num:100,7Min_num:1,8count:10009 }Ten return { OneGet:function(name) { A returnConf[name]? Conf[name]:NULL; - } - } the })(), - //Lazy Loading single case -Lazysingle: (function(){ - var_instance =NULL; + functionSingle () { - return { +Publicmethod:function(){}, APublicproperty: ' 1.0 ' at } - } - return function(){ - if(!_instance) { -_instance=Single (); - } in return_instance; - } to })() +}
Static variable Testing
1 var maxnum=jt.conf.get (' max_num '); 2 Console.log (Maxnum);
Lazy single-Case test
1 Console.log (Jt.lazysingle (). Publicproperty);
Console Display Results
javascript-Single-Case mode