Home >Web Front-end >JS Tutorial >Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-04-22 09:22:512026browse

This article will give you a detailed introduction to the concept and principles of JavaScript prototype chain inheritance. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

1. Prototype chain

  • The so-called prototype chain refers to inheritance through the prototype chain. The chain structure established between them is called the prototype chain.

  • When looking for an attribute of an object, it will first search from the private attributes of the object. If there is one, the corresponding value will be returned. If there is one, it will be along the of the object. _proto_ Search in the prototype. If it is not found in the prototype, continue to search upward along the prototype chain.

  • When the Object prototype is found, if there is still no property to be found, undefined is returned and the search stops. The reason why the Object prototype is found is because of all the construction methods At the bottom level, an inheritance relationship will be automatically established with Object, so that your prototype points directly to the prototype of Object.

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance
2. Format of the prototype chain

  格式:    function 构造方法1(形参1,形参2,形参3...){
        this.属性名1=形参1;
        this.属性名称2=形参2;
        ...
    }function 构造方法2(形参1,形参2, 形参3,...){
        this.属性名1=形参1;
        this.属性名称2=形参2 ;
        ...}
      构造方法2.prototype= new 构造方法1();
      构造方法2.prototype.constructor=构造方法2的名称

3. Illustration of the prototype chain
Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance
4. Things to note about prototype chain inheritance:

  • Prototype chain inheritance does not support multiple inheritance, but it does. Multi-level inheritance, and borrowing constructors supports multiple inheritance.

  • Multiple inheritance: The behavior of inheriting the binding and initialization functions of certain properties from multiple constructors at the same time is called multiple inheritance.

  • If you want to use prototype chain inheritance, the object needs to be created after inheritance. If it is before inheritance, the created object cannot use the content in the prototype of the superior constructor method.

Constructor method supports multiple inheritance
Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

##Prototype chain inheritance does not support multiple inheritance

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritanceIllustration of prototype chain inheritance does not support multiple inheritance

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance
##5. Illustration of the principle of prototype chain supporting multi-level inheritance Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance
6. If you want to use prototype chain inheritance, the object needs to be created after inheritance. If it is before inheritance, the created object cannot be used. Contents in the prototype of the superior constructor methodTake you one minute to understand the concept and principles of JavaScript prototype chain inheritance

The creation of the object needs to be done after inheritance

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

The object needs to be created after inheritance. If it is before inheritance, the created object cannot use the content in the prototype of the superior constructor method

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance

Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance
##[Recommended learning: Take you one minute to understand the concept and principles of JavaScript prototype chain inheritancejavascript advanced tutorial

The above is the detailed content of Take you one minute to understand the concept and principles of JavaScript prototype chain inheritance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete