8

Why does the following return false?

Object.prototype instanceof Object
14
  • 1
    Because Object.prototype was not made from the Object() function Commented Nov 16, 2014 at 21:33
  • 1
    But the funny thing is, Object.prototype.toString() === "[object Object]". Commented Nov 16, 2014 at 21:35
  • 1
    @Aravind: Function was not either, but is instanceof Object Commented Nov 16, 2014 at 21:35
  • It's also confusing because typeof Object.prototype is "object". Commented Nov 16, 2014 at 21:37
  • @Paul Draper: why is it confusing? It's just type (in terms of the language) of the object, not its ancestor name. Commented Nov 16, 2014 at 21:39

2 Answers 2

11

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.

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

5 Comments

Bro - do you have a blog explaining your feelings on OLOO vs. New etc.. and prototype etc.. I like the way you express/explain. very succinct. Any site of yours I can read?
@jamesemanon: No(t yet). But you can browse my SO answers on the topic of course :-)
@jamesemanon: To be honest, I've never heard the term OLOO before and had to google it. But yes, that seems to be the model of thought you should (or: I do) use in JS. I like its genericy, the concept of classes can be trivially implemented by it. Using constructor functions & new is just syntactic sugar (like ES6 classes are, but with more "common" syntax)
@Bergi I'm confused with your answer. I think you wanted to say b instanceof a instead of a instanceof b in your particular example.
@BhojendraNepal: No, it's really what I mean. a is the instance, b is the constructor.
0

Referencing MDN:

The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.

Comments

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.