Learning in JavaScript mode

Source: Internet
Author: User

Read a lot of online articles, basically get a conclusion: some write class tool functions or framework of the writing class is essentially a constructor + prototype

1. Use constructors to define class properties (fields).
2. Define the method of the class in a prototype way.

For specific articles, see

JavaScript writing style (1)

Http://www.cnblogs.com/snandy/archive/2011/03/06/1971764.html

First, let's look at the singleton mode

(1.) What does this website describe for a singleton mode?

(http://www.codingserf.com/index.php/2015/05/javascript-design-patterns-singleton/)

In JavaScript, the singleton pattern (the Singleton pattern) is very simple and simple enough to be ignored, but how it works at the technical level is something we need to know. The code for the Singleton is written in a separate object, so you don't need to instantiate a new object to use its resources whenever you need it, and the singleton allows access to its resources at the global level.
In JavaScript, we often use singleton mode when managing namespaces, which reduces the number of global variables you create in your code. Singleton mode is more useful in JavaScript than in other languages, because it can use namespaces to reduce the risk of global variables.

A basic single-case pattern

This is one of the most basic and simplest single-instance patterns implemented in JavaScript. It is a simple object literal with some methods and properties, which are supposed to be put together because of a relationship.

Conclusion: Managing namespaces using a singleton pattern is straightforward. You can also do this simply by creating an object literal :

(2.) What does this website describe for a singleton mode?

Http://frontenddev.org/link/javascript-design-mode-7-appearance.html

The singleton pattern means that only one instance of a class needs to be instantiated, and its method is relatively simple, by judging whether a class has been instantiated, and then returning the value. There are various ways to implement a singleton pattern,

Conclusion: In using the singleton model we can consider a few points to help you decide whether you should use this pattern to build your project:

    1. When a class can only be accessed by one instance, and it is easy to access it from a public access point.
    2. Instances of this class can be extended by subclasses, and customers can use an extended instance without changing the code.

(3.) What does this website describe for a singleton mode?

Http://blog.chinaunix.net/uid-26672038-id-3886959.html

In the case of Singleton mode, the generation of a singleton class must ensure that only one instance exists, and many times the whole system only needs to have a global object in order to coordinate the behavior of the whole system. For example, in the whole system configuration file, the configuration data has a singleton object for unified reading and modification, other objects need to configure the data also unified through the Singleton object to obtain configuration data, so as to simplify configuration management in a complex environment.

The idea of a singleton pattern is that a class can return a reference to an object (and is always the same) and a method that obtains the instance (a static method, usually using the getinstance name). So when we call this method, if the class holds a reference that returns the reference without being empty, the person creates an instance of the class, and assigns the instance reference to the reference that the class keeps and returns. The constructor of the class is also defined as a private method, so that other functions can use the constructor to instantiate the object, only the static method of the class to get the unique instance of the class. For JS, great flexibility makes it possible to implement singleton patterns in a variety of ways, using closures to simulate private data, which can be thought of as follows:Implementation 1: The simplest object literalImplementation 2: constructor internal judgmentImplementation 3: Closure mode

(4.) Another description

Http://www.cnblogs.com/yupeng/archive/2012/09/06/2673683.html

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

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

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

See here we can roughly summarize the characteristics of the singleton pattern:

With only one instance, you only need to have one global object

There are roughly 3 ways to achieve this

1: Simplest object literal

2: Constructor internal judgment

3: Closure mode

Learning in JavaScript mode

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.