Singleton and module modes in Javascript

Source: Internet
Author: User

In JavaScript,Singleton)An object with only one instance does not need to be created with the new keyword. A singleton is actually a bit similar to a static class in C #/C ++. In JavaScript, a singleton object is created in the JSON format of a key-Value Pair wrapped in curly brackets. For example:

 

VaR singletonobj = {
Name: 'john ',
Method: function (){
// Method code
}
};

Module ModeIt is used to create private variables and privileged methods (public methods) for a single instance, thus enhancing the accessibility of the single instance. Private variables and private functions defined in module mode can be accessed only by the privileged (public) method of the singleton object itself, but not by any external objects. The syntax format is as follows:

VaR Singleton = function (){
// Private variable
VaR privatevariable = 10;

// Private function
Function privatefunction (){
Return false;
}

// Return object
Return {
// Public attributes
Publicproperty: True,

// Public attributes and public methods
Publicmethod: function (){
Privatevariable ++;
Return privatefunction ();
}
};
}();

 

In the code above, we created an anonymous function and assigned the variable singleton. Then, we immediately call this function to return public object instances of public methods and attributes. Inside an anonymous function, a private variable and a private function are defined, and an object instance is returned as a function value. In this way, the returned object only contains public attributes and methods. Because the returned object is defined inside the anonymous function, its public methods and attributes can both access private variables and functions. This mode is suitable when you need to initialize a single instance and maintain private variables.

The call of Public attributes and public methods in a singleton object is the same as that of a common object and a method call. The public members of the singleton object are called using the dot notation or square brackets syntax. For example:

Alert (singleton. publicproperty );

Alert (Singleton [publicproperty]);

The execution result is true.

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.