“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

1. Instanceof

The instanceof operator is used to check whether the constructor’s prototype property appears on the prototype chain of an instance object.

function Person() {}
function Person2() {}

const usr = new Person();

console.log(usr instanceof Person); // true
console.log(usr instanceof Object); // true

console.log(usr instanceof Person2); // false
Copy the code

The above code defines two constructors, Person and Person2, and uses the new operation to create an instance object of Person, usr.

The instanceof operator is used to separately verify that the constructor’s prototype property is on the prototype chain of the instance usr.

Of course, it turns out that the Person and Object prototype properties are on usr’s prototype chain. Usr is not an instance of Person2, so the prototype attribute of Person2 is not on usr’s prototype chain.

2. Instanceof

After understanding the function and principle of Instanceof, you can implement a function with the same function as Instanceof:

function myInstanceof(obj, constructor) {
    // Implicit prototype of obj
    letimplicitPrototype = obj? .__proto__;// The prototype of the constructor
    const displayPrototype = constructor.prototype;
    // Iterate through the prototype chain
    while (implicitPrototype) {
        // Find, return true
        if (implicitPrototype === displayPrototype) return true;
        implicitPrototype = implicitPrototype.__proto__;
    }
    // If not found at the end of the loop, return false
    return false;
}
Copy the code

The myInstanceof function takes two arguments: the instance object obj and the constructor.

First get the implicit prototype of the instance object: obj.__proto__, the constructor’s prototype object constructive.prototype.

Then, we can get implicit archetypes at the next level by constantly getting them:

implicitPrototype = implicitPrototype.__proto__;
Copy the code

If displayPrototype is on the prototype chain, return true.

When implicitPrototype is null, the search ends and false is returned.

A prototype chain is essentially a data structure similar to a linked list.

All Instanceof does is look for a target node on a linked list. Starting at the head of the table and iterating backwards, return true if the destination node is found. If not found at the end of traversal, return false.

3. Verify

Write a simple example to verify your implementation of instanceof:

function Person() {}
function Person2() {}

const usr = new Person();

function myInstanceof(obj, constructor) {
    letimplicitPrototype = obj? .__proto__;const displayPrototype = constructor.prototype;
    while (implicitPrototype) {
        if (implicitPrototype === displayPrototype) return true;
        implicitPrototype = implicitPrototype.__proto__;
    }
    return false;
}

myInstanceof(usr, Person); // true
myInstanceof(usr, Object); // true

myInstanceof(usr, Person2); // false
myInstanceof(usr, Function); // false

myInstanceof(usr.__proto__, Person); // false
usr.__proto__ instanceof Person; // false
Copy the code

As you can see, myInstanceof got the result right.

Interestingly, usr.__proto__ instanceof Person returns false, indicating that obj instanceof constructor detects a prototype chain that does not include the obj node itself.


Common handwritten code in JavaScript:

“Making – code – js”