JavaScript single case/monomer mode (Singleton) _javascript tips

Source: Internet
Author: User
Tags closure event listener
Three features of a single case pattern:
1, the class has only one instance
2, the class creates the instance itself (the instance object inside the class creates itself)
3, exposing this instance interface to the entire system
That's probably what it looks like in Java.
Copy Code code as follows:

Class Singleton {
Private, static instances of the class itself
private static Singleton instance = new Singleton ();
Private constructors (constructors, constructors, construction methods)
Private Singleton () {}
Open, Static Factory method
public static Singleton getinstance () {
return instance;
}
}

When used
Copy Code code as follows:

Singleton obj = singleton.getinstance ();

This singleton class is instance when it is loaded, even if the loader is static. Therefore, for resource-intensive, configuration-expensive monomers it is more reasonable to defer instantiation (new) until it is used. Lazy Load (Lazy loading), which is commonly used for those monomers that must load large amounts of data. Modified under
Copy Code code as follows:

Class Lazysingleton {
Initially null, temporarily not instantiated
private static Lazysingleton instance = NULL;
Private constructors (constructors, constructors, construction methods)
Private Lazysingleton () {}
Open, static factory methods that need to be used only to create the monomer
public static Lazysingleton getinstance () {
if (instance = = null) {
Instance = new Lazysingleton ();
}
return instance;
}
}

Use the same way as above.
The single case pattern is one of the most basic and useful patterns of JavaScript. It provides a means of organizing code into a logical unit that is accessed through a single variable.
Monomers have many uses in Javascipt, which can be used to partition namespaces to reduce the overflow of global variables. You can also use the branching technique to handle differences in browsers.
There are many ways to implement a single example pattern in JavaScript, each of which has its own advantages or disadvantages.
1, the object of direct quantity to achieve the most basic, simplest monomer
Copy Code code as follows:

var Singleton = {
Attr1:1,
ATTR2: ' Hello ',
Method1:function () {alert (THIS.ATTR2);},
Method2:function (ARG) {}
}

In this way, all members of the object are accessed through the singleton dot number. All members are public, without private. When you execute to the variable singleton, you load (instantiate) itself, that is, non-lazy loading.
In addition, there are some risks method1 using this to access other members of the monomer because the context of method1 does not always point to the singleton object.
For example, when you use METHOD1 as an event listener, this may point to a DOM element, which may prompt undefined.
2, the closure realizes the private member's monomer
Copy Code code as follows:

var Singleton = function () {
var attr = 1, fn = function () {};
return {
Method:function () {fn ();},
Getattr:function () {return attr;}
};
}();

In this way Var defines the private member property attr, the method FN, and then returns a public interface, methods and GetAttr. In the future, when the implementation is modified, the interface methods method and GetAttr are invariant, only the private attr and the specific implementation of FN are modified. Use the following
Copy Code code as follows:

Singleton.method ();
Singleton.getattr ();

3, the closure realizes the private member's inert instantiation monomer
Copy Code code as follows:

var Lazysingleton = function () {
var attr = 1, fn = function () {};
var obj = {
Method:function () {fn ();},
Getattr:function () {return attr;}
};
function init () {
return obj;
}
return {getinstace:init};
}();

The application has already mentioned that the monomer that must load a large amount of data will not be instantiated until it needs to be used. This is how it works.
Copy Code code as follows:

Lazysingleton.getinstance (). method ();
Lazysingleton.getinstance (). GetAttr ();
Related Article

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.