Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Object.prototype instanceof Object false?

Why does the following return false?

Object.prototype instanceof Object
like image 798
Ben Aston Avatar asked Nov 16 '14 21:11

Ben Aston


People also ask

How do you check if an object is an instance of a class JavaScript?

The JavaScript instanceof operator is used to check the type of an object at the run time. It returns a boolean value(true or false). If the returned value is true, then it indicates that the object is an instance of a particular class and if the returned value is false then it is not.

What is difference between Typeof and Instanceof?

typeof: Per the MDN docmentation, typeof is a unary operator that returns a string indicating the type of the unevaluated operand. instanceof: is a binary operator, accepting an object and a constructor. It returns a boolean indicating whether or not the object has the given constructor in its prototype chain.

How do you find the prototype of an object?

The prototype of an object is referred to by the prototype property of the constructor function that creates and initializes the object. The isPrototypeOf() method provides a way to determine if one object is the prototype of another. This technique can be used to determine the class of an object.

Is Array an object Instanceof?

Short answer: Array instanceof Object is true because functions are objects and every object has Object.


Video Answer


1 Answers

Because it basically asks whether Object.prototype does inherit from Object's .prototype object: It does not.

a instanceof b is equivalent to b.prototype.isPrototypeOf(a) - it tests whether b.prototype is in the prototype chain of a. In your case, it is not in the chain, because it is the start of the chain itself. isPrototypeOf is not reflexive.

like image 160
Bergi Avatar answered Oct 25 '22 00:10

Bergi