Why does the following return false?
Object.prototype instanceof Object
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.
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.
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.
Short answer: Array instanceof Object is true because functions are objects and every object has Object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With