js prototype chain

The object created using the new constructor is an instance of the class, the __proto__ attribute of the instance points to the prototype object, the prototype attribute of the constructor also points to the prototype object, and the __proto__ of the prototype object points to the prototype object it inherits.

The __proto__ property of each prototype object points to its inherited prototype object (the prototype property of the parent class constructor). The prototype of the constructor points to the prototype object. The prototype object has two properties, constructor and __proto__. The constructor points to the constructor, and __proto__ points to the prototype object of the prototype object.

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>原型链</title>
</head>
<body>

</body>
<script>
    function Parent() {
        this.name="parent";
    }
    Parent.prototype.say=function () {
        console.log("say");
    }
    function Child() {
        Parent.call(this);
        this.type="Child";
    }
    Child.prototype=Object.create(Parent.prototype);
    Child.prototype.constructor=Child
    var p=new Parent(),c=new Child();
    console.log(p,c);
</script>
</html>

 

In the above code, we wrote a Parent class and a Child class. The Child class inherits from the Parent class, and then creates a new p instance (p=new Parent()) and c instance (c=new Child()).

1.Parent is called the constructor, and we generally call this class the Parent class.

2.p is called an instance of the Parent class.

3. We can see that the result of Parent.prototype===p.__proto__ is true, Parent.prototype and p.__proto__ are strictly equal, and their pointers are called prototype objects .

4. The constructor of an instance is strictly equal to the constructor of his prototype object (the result of c.constructor===Child.prototype.constructor is true)

5. The prototype object is also an object. If it is an object, it has the __proto__ property, and the __proto__ property of the prototype object of the special Object object is empty.

6. All objects on the prototype chain ultimately point to Object.prototype, and Object does not inherit from any object , so his prototype object has no prototype object (Object.prototype.__proto__===null).

7. The prototype object of instance c is c.__proto__, and the prototype object of c.__proto__ is c.__proto__.__proto__, which is Parent.prototype (c.__proto__.__proto__===Parent.prototype result is true),

   The prototype object of c.__proto__.__proto__ is c.__proto__.__proto__.__proto__ that is Object.prototype (the result of c.__proto__.__proto__.__proto__===Object.prototype is true),

   We can find that we can always look up prototype objects through the __proto__ property, just like all prototype objects are bound to a chain, we can look up this chain, this is the prototype chain !

instanceof operation

The instanceof operation is to search along the prototype chain. The operation process of c instanceof Object is to first find c's prototype object c.__proto__, which is not equal to Object.prototype, so it looks for c.__proto__.__proto__, which is not equal to Object. prototype, then find c.__proto__.__proto__.__proto__, which is equal to Object.prototype, and the final result returns true. Returns false if none of the prototype objects on the prototype chain are equal during this lookup.

According to this search method, the results of c instanceof Child and c instanceof Parent are all true, because the prototype objects of Child and Parent are both on the prototype chain. As shown

 

可以看出如果用instanceof来判断一个实例所属的类可能会出错,那么我们用什么方法来判断一个实例具体属于哪个类呢?

注意不同的类的构造函数是不同的,所以我们可以根据constructor属性来进行判断。

c.constructor===Child为true(注意Child指的是构造函数,我们一般也把Child叫Child类),c.constructor===Child.prototype.constructor为true(上面第4条),c.constructor===Parent为false,如图

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324524375&siteId=291194637