noob-in-need
noob-in-need

Reputation: 921

How is it that (Object instanceof Function) and (Function instanceof Object) are both true?

Wait! I know there are other very similar questions, but (perhaps it's me) I need a specific part of it answered.

I get that one could just say Object.prototype is at the very top of the delegation chain. But, how is it that Object (as a function object) can exist to have a prototype method on it before Function exists to make it an instance? Should I just imagine some voodoo where they're both created simultaneously?

Both Object instanceof Function and Function instanceof Object are true

It seems chicken and egg.

Upvotes: 3

Views: 143

Answers (1)

Pointy
Pointy

Reputation: 413826

(Object instanceof Function)

is true because the Object constructor is, in fact, a function.

(Function instanceof Object)

is true because the Function constructor is a function, and all functions are objects.

Note that it's also true that

(Object instanceof Object)

and

(Function instanceof Function)

The left-hand expression is checked to see whether the right-hand constructor function's prototype is in its prototype chain. Note that that check does not involve looking at the "prototype" property of the left-hand side; that's irrelevant. What counts is the prototype chain of the left side evaluated as an object instance of some sort; the "prototype" property of an object instance has no particular meaning.

Thus in all of the seemingly quirky tests above, the left-hand side values are interpreted as being simply function instances. The fact that they're the particular functions they are really doesn't have any effect on the outcome.

Upvotes: 5

Related Questions