Javascript Singleton mode and javascript Mode

Source: Internet
Author: User

Javascript Singleton mode and javascript Mode

The Singleton mode is one of the most basic and common patterns in the design mode. It features that a single object only has one instance, in this way, you can ensure that the same global resources are used in all your code.
In Java, the method for implementing the singleton mode is to first privatize the constructor, create a private static variable to save the instance, and then use a static method (usually named getInstance) provide the instance to external access. Because the constructor is private, classes cannot be instantiated externally, but can only be obtained through getInstance. Therefore, there is only one static instance from the beginning to the end.
We can also use this idea in Javascript.

(1) using direct object quantity
var Single = {    name: "zhangsan",    age: 20}

Directly creating objects through objects is indeed the simplest Singleton implementation method, which ensures that there is only one Single object in the world, however, the object created in this way is "portal wide open" and cannot create its private members (of course, You can underline the name of the Private member to be set as shown in Figure_nameTo imply that it is private, but this is not a real privatization ). In the previous blog, we discussed how to create a private static member, which can be implemented through a closure.

Use closures
Var Single = (function () {// Private member var age = 0; function priMethod (){//...} return {// public member name: "zhangsan", getAge: function () {return age ;}, setAge: function (_ age) {age = _ age ;}}}) ();

In the above example, we get an object by executing the function immediately. This object is still unique, but it has private attributes and methods.

(2) objects created through new

Since we can privatize members through closures, we can certainly imitate Java to make objects private and static, and then pass them through the getInstance method, which is similar to the implementation in java. The Code is as follows:

Var Single = (function () {// unique instance var single = new function () {// Public Member this. name = "zhangsan"; this. say = function () {console. log (this. name) ;}} return {getInstance: function () {return single ;}}) (); var s1 = Single. getInstance (); var s2 = Single. getInstance (); console. log (s1 = s2); // true

Actually, I am familiar with closures. The above principles are similar, but I only use the instance as a static private member.

Inert Loading

Regardless of the method above, they share a common feature: a single object is created when a script is loaded. for very large objects, the overhead is very high, we can postpone its Instantiation to the loading when it is needed, which is called inert loading.

Var Single = (function () {// unique instance var single; // private Constructor () {// Public Member this. name = "zhangsan";} return {getInstance: function () {if (! Single) {// If single does not exist, create single = new Constructor () ;}return single ;}}})();

Externally, we use the getInstance Method for access:

var s1 = Single.getInstance();var s2 = Single.getInstance();console.log(s1 === s2);  //true

In this way, an instance is created only when the instance is used.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.