There are several modes in js that can create objects, and operate the properties and methods contained in the object.
Generally speaking, the first letter of the constructor name is a capital letter, and the first letter of the non-constructor name is a lowercase letter. Of course, the only difference between a constructor and a general function is the difference in the way of calling. Therefore, 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 general function.
Let me talk about my understanding of these patterns:
Factory mode: Create a general function, create an Object object in the function, add attributes and methods to the object, assign its value, and finally return the object. The object type cannot be recognized.
Constructor mode: Create a constructor, use this to assign values, and whenever an instance is created, the method is created once, and each method executes the same command, which is redundant. This disadvantage can be done by putting the method into the global environment, but this way there is no encapsulation. However, it can be solved through prototype mode.
Prototype pattern: Each function has a prototype property, which is a pointer, pointing to an object that contains properties and methods shared by all instances created by its function.
The relationship between prototype objects, constructors and instances is as follows:
Illustration: 1: The constructor and the instances created by the constructor, their prototype attributes point to the constructor's prototype object.
2: The prototype object of the constructor has the 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 the object literal, the constructor points to the object constructor. If it is required to point to another constructor, you need to modify the value of the constructor attribute of the prototype object, such as: constructor: Person. In this way, even if the Person prototype object is rewritten, the constructor of the prototype object still points to the Person constructor.
When creating an instance first: If you add properties or methods directly, the instance can be accessed.
If the prototype object is rewrite, the prototype of the constructor points to the new prototype object, while the prototype of the instance created previously 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 instances, and parameters cannot be passed, which is not what we want. Each instance has common information and different information, so we can use a combination of constructor mode and prototype mode.
Use a combination of constructor mode and prototype mode:
State prototype pattern: combine independent constructors with their prototype objects, initialize the prototype in the constructor, and add methods to it.
If the method does not exist, it is added to the prototype object and is executed only when the prototype is initialized, and only once.
Parasitic constructor mode: Similar to factory mode, the difference is: the parasitic constructor mode is a constructor, and instances are created through new.
Stable constructor pattern: There are no public properties, and its methods do not refer to this object. No new is used when creating an instance. Only access to properties (i.e., incoming data) is done through methods.
The above introduction to several modes of javascript creation objects is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.