Reading Notes in Javascript design patterns-Single Mode

Source: Internet
Author: User

The single object is created when the script is loaded. The following code:

Var Singleton = (function () {// common standalone Mode
// Code here

Return {

};
})();
For resource-intensive or configuration-intensive monomer, it may be more reasonable to postpone its instantiation until it needs to be used. This technology is called "lazyloading", which is most commonly used for the units that must load a large amount of data. It is better to instantiate the tool that is used as a namespace, a specific code package for a specific webpage, or a practical method for organizing the relevant methods.
The special feature of this kind of inert loading monomer is that access to them must rely on a static method. The method Singleton. getInstance (). methodName () should be called like this instead of Singleton. methodName (). The getInstance method checks whether the monomer has been instantiated. If not, it creates and returns its instance. If the monomer has been instantiated, it returns the existing instance.

Compare the common monomer mode with the inert loaded monomer mode:

Var Singleton = (function () {// standalone mode for inert Loading
Var uniqueInstance = null;

Function constructor (){
// Code here

Return {

};
}

Return {
GetInstance: function (){
If (! UniqueInstance ){
UniqueInstance = constructor ();
}
Return uniqueInstance;
}
};
})();

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.