javascript-Single-Case mode

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.