What the difference between Array.prototype.isPrototypeOf and Array.isPrototypeOf?

I am wondering what is the difference between Array.prototype.isPrototypeOf and Array.isPrototypeOf I thought it should work the same because I thought it will refer to the same method isPrototypeOf but It’s look like I was mistaken. Can anybody explain to me why this work like that ?

const exampleArray = [1, 2, 3];
console.log(Array.isPrototypeOf(exampleArray)); // Why this statement returns false ?

Those are both references to
Object.prototype.isPrototypeOf(), which checks to see if the object it’s called on is in the prototype chain of the passed argument.

For the exampleArray, the prototype chain is this:

Object.prototype <- Array.prototype <- exampleArray instance

See snippet:

const exampleArray = [1, 2, 3];
  Object.getPrototypeOf(exampleArray) === Array.prototype,
  Object.getPrototypeOf(Array.prototype) === Object.prototype

The Array constructor functionwindow.Array – is not in the prototype chain, so isPrototypeOf returns false.

The Array constructor function would only have isPrototypeOf return true if a class extended Array, or if it was set to be the internal prototype of a new object via Object.create, eg:

class ExtendedArray extends Array {}

const somethingWeird = Object.create(Array);

For completeness, the Array constructor function – being a function – inherits from Function.prototype, which inherits from Object.prototype:

  Object.getPrototypeOf(Array) === Function.prototype,
  Object.getPrototypeOf(Function.prototype) === Object.prototype

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   How to call a function after a div is ready?

Similar Posts