JS Single Example Mode Detailed example _ Basic knowledge

Source: Internet
Author: User

What is a single case?

A singleton requires that a class has and only one instance, providing a global access point. So it has to bypass the regular controller so that it can only have one instance for the user to use, while using a few instances of not caring, so this is the designer's responsibility

Copy Code code as follows:

In JavaScript, singletons serve as a shared resource namespace which isolate code from the global implementation So as to provide a single point of access for functions.

In JavaScript, a single instance is treated as a global namespace, providing a point to access the object.

Working with scenes

Copy Code code as follows:

In practice, the Singleton-useful when exactly one object is needed to coordinate others a system.

The single example comparison applies to an object interacting with other systems.

Analogy

A single example is somewhat similar to a group of team leaders, in a period of time only a leader, there are team leaders to designate the work of members, distribution and coordination and team members work.

Example 1: This is the simplest single example of storing properties and methods in the form of Key,value

Copy Code code as follows:

var A = {
Xx:3,
Yy:4,
B:function (EL) {

},
C:function (EL) {

},
D:function (EL) {

},
E:function (EL) {

}
}


Example 2: First create a reference to an instance, and then determine whether the instance exists, if it does not exist then create it, if it exists, then return directly, guaranteed to have and only one.

Copy Code code as follows:

var Mysingleton = (function () {

Instance stores a reference to a singleton instance
var instance;

function init () {

Singleton

Private methods and variables
function Privatemethod () {
Console.log ("I am Private");
}

var privatevariable = "Im also private";

return {

Methods and variables in common
Publicmethod:function () {
Console.log ("The public Can-me!");
},

Publicproperty: "I am also public"
};

};

return {

If the instance does not exist, then create a
Getinstance:function () {

if (!instance) {
Instance = init ();
}

return instance;
}

};

})();

var Singlea = Mysingleton;
var singleb = Mysingleton;
Console.log (Singlea = = = Singleb); True

Example 3:

Copy Code code as follows:

var Singletontester = (function () {
Options:an object containing configuration options for the singleton
e.g var options = {name: "Test", pointx:5};
function Singleton (options) {
Set options to the options supplied
Or an empty object if none are provided
options = Options | | {};
Set some properties for our singleton
THIS.name = "Singletontester";
This.pointx = Options.pointx | | 6;
This.pointy = Options.pointy | | ;
}
Our instance Holder
var instance;
An emulation of static variables and methods
var _static = {
Name: "Singletontester",
method to getting an instance. It returns
A singleton instance of a singleton object
Getinstance:function (options) {
if (instance = = undefined) {
Instance = new Singleton (options);
}
return instance;
}
};
return _static;
})();
var singletontest = singletontester.getinstance ({
Pointx:5
});
Log the output of pointx just to verify it is correct
Outputs:5
Console.log (SINGLETONTEST.POINTX);

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.