Home  >  Article  >  Web Front-end  >  javascript singleton/singleton_javascript skills

javascript singleton/singleton_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:08:151026browse

Three characteristics of the singleton pattern:
1. The class has only one instance
2. The class creates the instance by itself (creates its own instance object inside the class)
3. Exposes this to the entire system The instance interface
looks like this in Java

Copy code The code is as follows:

class Singleton {
//Private, static instance of the class itself
private static Singleton instance = new Singleton();
//Private constructor (constructor, constructor, constructor)
private Singleton(){}
//Public, static factory method
public static Singleton getInstance() {
return instance;
}
}

Use When
Copy code The code is as follows:

Singleton obj = Singleton.getInstance();

This singleton class instance will be instantiated when itself is loaded, even if the loader is static. Therefore, for resource-intensive and configuration-intensive monoliths, a more reasonable approach is to defer instantiation (new) until the time it is used. That is, lazy loading (Lazy loading), which is often used for monoliths that must load large amounts of data. Modify
Copy code The code is as follows:

class LazySingleton {
//Initially: null, not instantiated yet
private static LazySingleton instance = null;
//Private constructor (constructor, constructor, constructor method)
private LazySingleton(){}
//Public , a static factory method, create the singleton when needed.
public static LazySingleton getInstance() {
if( instance == null ) {
instance = new LazySingleton();
}
return instance;
}
}

The usage is the same as above.
The singleton pattern is one of the most basic and useful patterns in Javascript. It provides a means of organizing code into a logical unit that is accessed through a single variable.
Singles have many uses in Javascript and can be used to divide namespaces to reduce the proliferation of global variables. It can also be used in branching techniques to handle differences between browsers.
There are many ways to implement the singleton pattern in Javascript, each of which has its own advantages or disadvantages.
1. Object literal implementation is the most basic and simplest singleton
Copy code The code is as follows:

var Singleton = {
attr1 : 1,
attr2 : 'hello',
method1 : function(){alert(this.attr2);},
method2 : function( arg){}
}

In this method, all members of the object are accessed through Singleton plus dot. All members are public, nothing private. When the variable Singleton is executed, it will load (instantiate) itself, that is, it is not lazy loaded.
In addition, there are some risks in method1 using this to access other members of the singleton, because the context of method1 does not always point to the Singleton object.
For example, when method1 is used as an event listener, this may point to a dom element, and undefined may be displayed.
2. Closure implements the singleton of private members
Copy the code The code is 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 attribute attr, method fn, and then returns a public interface method and getAttr. When the implementation is modified in the future, the interface methods method and getAttr remain unchanged, and only the specific implementation of private attr and fn needs to be modified. Use the following
to copy the code The code is as follows:

Singleton.method();
Singleton .getAttr();

3. Closure implements lazy instantiation of private members as a singleton
Copy code The code is 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};
}();

Applicable occasions have been mentioned above: for those single entities that must load a large amount of data, they are not instantiated until they are needed to use it. The usage is like this
Copy the code The code is as follows:

LazySingleton.getInstance().method ();
LazySingleton.getInstance().getAttr();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn