Home > Web Front-end > JS Tutorial > Introduction to several modes of creating objects in javascript_Basic knowledge

Introduction to several modes of creating objects in javascript_Basic knowledge

WBOY
Release: 2016-05-16 15:01:53
Original
1546 people have browsed it

There are several modes in js to create objects and operate the properties and methods contained in the objects.

Generally speaking, the first letter of the constructor name is an uppercase letter, and the first letter of the non-constructor function name is a lowercase letter. Of course, the only difference between a constructor and a general function is the way of calling it, so As long as any function is called through new, it can be used as a constructor. If it is not called through new, it is the same as a normal function.

Talk about my understanding of these modes:

Factory mode: Create a general function, create an Object object in the function, add properties and methods to the object, assign its value, and finally return the object. Unrecognized object type.

Constructor pattern: Create a constructor, use this to assign values, each time an instance is created, the method is created once, and each method executes the same command , which is redundant. This shortcoming can be solved by placing the method in the global environment, but then there is no encapsulation. But it can be solved through prototype mode.

Prototype pattern: Every function has a prototype attribute, which is a pointer to an object that contains properties shared by all instances created by its function. method.

The relationship between prototype objects, constructors and instances is as follows:

 

Illustration: 1: Constructors and instances created by constructors, their prototype attributes point to the prototype object of the constructor.

2: The prototype object of the constructor has a constructor attribute, which points to the constructor.

3: All properties and methods contained in the prototype object of the constructor can be shared by all instances created by the constructor.

After rewriting the prototype object using object literals, the constructor points to the object constructor. If you need it to point to another constructor, you need to modify the value of the constructor attribute of the prototype object, such as: constructor: Person, so that the prototype of Person Even if the object is overridden, the constructor of the prototype object still points to the Person constructor.

When creating an instance first: If attributes or methods are added directly, the instance can be accessed.

If the prototype object is overridden, 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 the new properties or new methods of the new prototype object. .

The prototype object contains shared properties and methods, so each instance has this information, so there is no difference between the instances, and parameters cannot 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 in combination.

Combined use of constructor pattern and prototype pattern:

                                                                    Static prototype mode: Combine an independent constructor with its prototype object, initialize the prototype in the constructor, and add methods to it.


                                                                                       

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

Parasite constructor pattern: Similar to the factory pattern, the difference is: the parasitic constructor pattern is a constructor and creates instances through new. Sure constructor pattern: There are no public properties, and its methods do not reference this object. Do not use new when creating an instance. Properties (i.e. the data passed in) can only be accessed through methods.

The above introduction to several modes of creating objects in JavaScript is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home more.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template