A prototype object is also a common object and may have its own prototype. If the prototype of a prototype object is not nu
A prototype object is also a common object and may have its own prototype. If the prototype of a prototype object is not null, we call it a prototype chain ).
A prototype chain is a finite chain of objects which is used to implemented inheritance and shared properties.
Prototype chain is a finite object chain composed of objects because of its inheritance and sharing attributes.
Imagine a situation where most of the two objects are the same and only a small part is different. Obviously, in a good design model, we need to reuse the same part, instead of repeatedly defining the same methods or attributes in each object. In A system based on class [class-based], these reusable parts are called class inheritance-put the same part into class A, and then class B and class C inherit from class, you can declare that you have something unique.
ECMAScript does not have the concept of a class. However, the concept of reusing [reuse] is no different (in some aspects, or even more flexible than class-) and can be implemented by prototype chain. This inheritance is called delegation based inheritance-based delegation, or more commonly, prototype inheritance.
Similar to "A", "B", and "C", create an object class "a", "B", and "c" in ECMAScript. Correspondingly, the object "a" has a common part of the object "B" and "c. Meanwhile, objects "B" and "c" only contain their own additional attributes or methods.
Var a = {x: 10, calculate: function (z) {return this. x + this. y + z }}; var B = {y: 20, _ proto __: a}; var c = {y: 30, _ proto __: }; // call the inherited method B. calculate (30); // 60c. calculate (40); // 80
It seems very easy. B and c can use the calculate method defined in a, which is implemented by the original type chain [prototype chain.
The principle is simple: if the calculate method cannot be found in object B (that is, the calculate attribute is not found in object B), it will be searched along the prototype chain. If the calculate method is not found in prototype of B, the prototype of a is found along the prototype chain and the entire prototype chain is traversed. Remember, once found, the first property or method is returned. Therefore, the first property is an inherited property. If the entire prototype chain is still not found, undefined is returned.
Note that this value still points to the object it originally belongs to in an inheritance mechanism, rather than the object it belongs to when it is found on the prototype chain. For example, in the above example, this. y is obtained from B and c, not. Of course, you also found that this. x is obtained from a, because it is found through the prototype chain mechanism.
If the prototype of an object has not been declared or defined, the default value of _ prototype _ is object. prototype, and object. prototype also has a _ prototype __, which is the end point of the prototype chain and is set to null.
The following figure shows the inheritance relationships of a, B, and c:
Prototype chain
A prototype chain is usually used in this case: the object has the same or similar state structure (same or similar state structure) (that is, the same property set) different Status values (different state values ). In this case, we can use Constructor to create an object in a specific mode (specified pattern.
Additional reading
The topic list of this article is as follows:
- How should we understand the working principle of the JavaScript engine?
- JavaScript exploration: the importance of writing maintainable code
- JavaScript exploration: exercise caution when using global variables
- JavaScript exploration: var pre-parsing and side effects
- JavaScript exploration: for Loop (for Loops)
- JavaScript exploration: for-in loop (for-in Loops)
- Exploring JavaScript: Prototypes is too powerful
- JavaScript: eval () is the devil"
- JavaScript exploration: Using parseInt () for Numerical Conversion
- Exploring JavaScript: Basic coding specifications
- JavaScript exploration: function declaration and function expression
- JavaScript exploration: Name function expressions
- JavaScript: function name in the debugger
- JavaScript: JScript Bug
- JavaScript exploration: Memory Management of JScript
- Exploring JavaScript: SpiderMonkey's quirks
- JavaScript exploration: an alternative solution to naming function expressions
- JavaScript exploration: Object
- JavaScript exploration: Prototype chain
- JavaScript exploration: Constructor
- JavaScript probing: executable context Stack
- Execution context 1: Variable object and activity object
- Execution context 2: Scope chain Scope Chains
- Execution context 3: Closure Closures
- Execution context 4: This pointer
- Exploring JavaScript: Powerful prototype and prototype chain
- JavaScript Functions 1: function declaration
- JavaScript function 2: function expressions
- JavaScript function 3: function expressions in a group
- JavaScript function 4: function Constructor
- JavaScript variable object 1: VO Declaration
- JavaScript variable object 2: VO in different execution contexts
- JavaScript variable object 3: two stages of execution Context
- JavaScript variable object IV: Variables
- Property of the JavaScript variable object __parent _
- JavaScript scope chain 1: Scope chain Definition
- JavaScript scope chain 2: function Lifecycle
- JavaScript scope chain 3: Scope chain features
- JavaScript closure 1: Introduction to closures
- JavaScript closure 2: Implementation of closure
- JavaScript closure 3: Closure usage
Address of this article: http://www.nowamagic.net/librarys/veda/detail/1641,welcome.