This mind map is my summary of the object, prototype, prototype chain and other knowledge, the main reference elevation book sixth chapter, after writing to find so much, may be streamlined later. There may be errors in the content, and you are welcome to criticize. Download ==>github
ECMAScript supports object-oriented (OO) programming, but does not use classes or interfaces. objects can be created and enhanced during code execution, so they are dynamic rather than strictly defined entities. In the absence of classes, the following patterns can be used to create objects.
- Factory mode, use simple functions to create objects, add properties and methods to objects, and then return objects. This pattern was later superseded by the constructor pattern.
- constructor mode, you can create custom reference types and use the new operator as you would create a built-in object instance. However, the constructor pattern also has the disadvantage that each of its members cannot be reused, including functions. Since functions can be not confined to any object (that is, loosely coupled to an object), there is no reason not to share functions among multiple objects.
- Prototype mode, using the prototype property of the constructor to specify the properties and methods that should be shared. When combining constructor and prototype patterns, use constructors to define instance properties, and use prototypes to define shared properties and methods.
JavaScript implements inheritance primarily through the prototype chain. The construction of a prototype chain is accomplished by assigning an instance of one type to the prototype of another constructor. In this way, subtypes have access to all properties and methods of the superclass, which is similar to class-based inheritance. The problem with the prototype chain is that the object instance shares all inherited properties and methods, so it is not appropriate to use it alone. The technique for solving this problem is to borrow the constructor, which is to call the superclass constructor inside the subtype constructor. This allows each instance to have its own properties, while also guaranteeing that only the constructor pattern is used to define the type. The most used inheritance pattern is composite inheritance, which uses the prototype chain to inherit shared properties and methods, while inheriting instance properties by borrowing constructors. In addition, the following alternative inheritance modes exist.
- Prototype inheritance, which can be implemented without having to pre-define constructors, is essentially a shallow copy of a given object. The copied copy can also be further modified.
- Parasitic inheritance, very similar to archetypal inheritance, is the creation of an object based on an object or some information, and then the enhancement of the object, and finally the return object. This pattern can be used in conjunction with composite inheritance in order to solve inefficient problems caused by multiple calls to the superclass constructor by the composite inheritance mode.
- Parasitic combined inheritance, combining the advantages of parasitic inheritance and combinatorial inheritance, is the most effective way to implement type-based inheritance.
JavaScript object, prototype, prototype chain knowledge summary mind Mapping