JavaScript create a few patterns of the object introduction _ Basics

Source: Internet
Author: User
Tags constructor

There are several modes in JS that can create objects and manipulate the properties and methods contained in the object.

In general, the first letter of a constructor name is a capital letter, the first letter of the non-constructor name is lowercase, and of course the only difference between the constructor and the normal function is only the way it is invoked, so that any function can be invoked as a constructor if it is called by new. , it is the same as a normal function.

Talk about my understanding of these patterns:

Factory mode: Create a generic function that creates an object in the function, adds properties and methods to the object, assigns its value, and returns the object at the end. Object type not recognized.

constructor Pattern: creates a constructor, uses this to assign a value, and whenever an instance is created, the method is created once, and each method executes the same command, which is superfluous. This disadvantage can be achieved by putting the method into the global environment, but there is no encapsulation. However, it can be solved by prototyping mode.

prototype mode: each function has a prototype property, which is a pointer to an object that contains properties and methods shared by all instances of its function creation.

The relationship between the prototype object, the constructor, and the instance is as follows:

Diagram: 1: Constructors and instances created by constructors, their prototype properties all point to the stereotype object of the constructor.

2: The stereotype object of the constructor has the constructor property, which points to the constructor.

3: The stereotype object of the constructor contains all the properties and methods that can be shared by all instances created by the constructor.

When you rewrite a prototype object with an object literal, constructor points to the object constructor, and if you need it to point to another constructor, you modify the value of the constructor property of the prototype object, such as: Constructor:person, The constructor of the prototype object still points to the person constructor, even if it is rewritten.

When you start creating an instance: If you add a property or method directly, the instance can be accessed.

If you rewrite a prototype object, the prototype of the constructor points to the new prototype object, and the prototype of the previously created instance still points to the original prototype object, so the instance cannot access new properties or new methods of the new prototype object.

A prototype object contains shared properties and methods, so each instance has this information, so that there is no difference between the instances, and no arguments can be passed, which is not what we want. There is common information and different information between each instance, so we can use the constructor pattern and the prototype pattern together.

The combination of a constructor pattern and a prototype pattern is used:

State prototype pattern: combines a separate constructor with its prototype object, initializes the prototype in the constructor, and adds a method to it.

If the method does not exist, it is added to the prototype object, executed only when the prototype is initialized, and only once.

Parasitic constructor pattern: Similar to factory pattern, the difference is: the parasitic constructor pattern is the constructor, and the instance is created by new.

Secure constructor Pattern: There is no public property whose method does not refer to the object of this. New is not used when creating an instance. Properties can be accessed only through methods (that is, incoming data).

The above JavaScript to create several patterns of the object is small set to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.