prototype - prototype chain

prototype - prototype chain

ps: suppose

function Person(){
    
    };
var per = new Person();

Very important: the object-oriented in js is based on the prototype (prototype)

  1. The attribute (method) defined on the prototype, through "inheritance" makes: the instance also has this attribute (method);

  2. When the constructor is declared in the function, it will generate a prototypeproperty (默认指向一个空的Object对象), and the constructor can prototypeaccess the prototype object.

  3. The instance __proto__accesses prototypethe same prototype object pointed to by the constructor through .

    • per.proto === Person.prototype // true
    • in:
      • prototype: also known as "display prototype";
      • proto : also known as "implicit prototype"

    • Note: Before ES6: Only the explicit prototype can be operated, and the implicit prototype cannot be operated.
  4. constructor : the prototype can constructoraccess the constructor through the constructor

// 实例访问构造函数
per.__proto__.constructor === Person;  //true
  1. Example read attribute process:
	1. 现在自身的属性查找
	2. 然后沿着 "__proto__" 隐式原型链查找
	3. 如果都没有则返回 undefined
  1. Prototype inheritance: the instance object automatically has the properties (methods) of the constructor prototype object ——— Principle: prototype chain

instanceof

A instanceof B : Whether the explicit prototype of the B function exists on the implicit prototype chain of the A instance.

some more important points

Function = new Function(); // Function 是由自身 new 出来的!牛掰吧!自己生了自己,有意思
// 所以
Function.__proto__ === Function.prototype; // true
// Object 是由Function new出来的,也就是说 Object.__proto__ === Function.prototype;
// 但是"__proto__"隐式原型链的最终指向还是会到达Object, 很绕对吧!!!
Object = new Function();

// 所以
Object.__proto__ === Function.prototype;
关于原型链的三个点:(原型链又称之为:隐式原型链)
1. 子类(实例)'__proto__' 指向父类(构造函数)'prototype'
2. 原型链(隐式原型链)的最终指向是 "Object.prototype"
3. Object.prototype.__proto__ === null 原型链的尽头

To sum up: Then such a picture is formed

Guess you like

Origin blog.csdn.net/cc_King/article/details/126468274