A single-instance pattern of JavaScript design Patterns

Source: Internet
Author: User

An interpretation of the concept of single-case model

1. Single-case model concept text interpretation

A single example is to ensure that a class has only one instance, the method of implementation is generally first to determine whether the instance exists or not, if there is a direct return, if it does not exist to create a return, this ensures that a class has only one instance object. In JavaScript, a singleton serves as a namespace provider, providing a unique access point from the global namespace to access the object.

2. Simple interest model concept of quasi-materialized interpretation

Buy a house first to confirm whether there is a door, not to contact the developer to create. Second, each door has its own ownership, 301 belong to Xiao Wang, 302 belong to Xiao Li, Xiao Wang Xiao Li belongs to the name space, but a building there are several small Wang Xiao Li, these global namespaces in the 301 belong to a small king, that is, provide a unique access point to access the object.

Second, the role of the single-case model and matters needing attention

Mode effect:

1. Inter-module communication.

2. There can be only one object for a class in the system.

3. Protect your own properties and methods.

Precautions:

1. Note the use of this (who is called, who points to).

2. Closures are prone to memory leaks, eliminating the need to quickly delete them.

3. Pay attention to the cost of new. Inheritance

The Code Combat and summary of the single case model

A single-instance pattern of JavaScript design Patterns

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.