3

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.

2
  • 1
    Looks like another one for wtfjs.com Commented Sep 3, 2015 at 15:22
  • 1
    I don't think this is very mysterious. Both the Object constructor and the Function constructor are functions, and they're both also objects. Commented Sep 3, 2015 at 15:26

1 Answer 1

5
(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.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't think so. Function instanceof Object is because Function.prototype delegates to Object.prototype. Object instanceof Function would then be because Object.prototype delegates to Function.prototype
@noob-in-need no, that's not correct. The prototype property of the left-hand expression is not in any way relevant.
You're right. Thanks. I see how I'm conflating things.
"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." This is exactly what I wasn't thinking about. Blah. Thanks again.
@noob-in-need glad to help out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.