instance of (js)

ref – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

User Object

Create Prototype Object and Connect

Whenever we want a object to derive from other objects, we derive their prototype functionalities.
We do so like this:

1) create the an object X with its property __proto__ pointing to the prototype object via Object.create.
2) have their function constructor’s prototype point to that object X.
3) have the object X’s constructor point back to the constructor function
4) Now that we are done connecting, we can use a callback to execute any prototype implementations.

Admin and SuperAdmin

Create the Admin constructor function, initialize its subclass, and then initiate its own property.
Since we want to derive from the user prototype, we put in Admin as the constructor, User.prototype as the prototype object we want to derive from, and finally, implement the prototype functionalities.

instanceof

The instanceof operator tests whether the prototype property of the [Constructor function] appears anywhere in the prototype chain of the [object].

instanceof is an operator and it expects two operands:

– an object
– Constructor function

it will test if the passed function prototype property exists on the chain of the object.

For example:

Diagram wise:

Hence, as we can see, for object u,

the prototype property of Admin appears in the prototype chain.
the prototype property of User appears in the prototype chain.
However, the prototype property of SuperUser DOES NOT appear

isPrototypeOf

the isPrototypeOf is a function available on the Object.prototype object, it lets you test if an specific object is in the prototype chain of another, since this method is defined on Object.prototype, it is be available for all objects.

for example…