8

Why does the following return false?

Object.prototype instanceof Object
Scimonster
  • 32,893
  • 9
  • 77
  • 89
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 1
    Because Object.prototype was not made from the Object() function – Aravind Nov 16 '14 at 21:33
  • 1
    But the funny thing is, `Object.prototype.toString() === "[object Object]"`. – soktinpk Nov 16 '14 at 21:35
  • 1
    @Aravind: `Function` was not either, but is `instanceof Object` – Bergi Nov 16 '14 at 21:35
  • It's also confusing because `typeof Object.prototype` is `"object"`. – Paul Draper Nov 16 '14 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. – zerkms Nov 16 '14 at 21:39
  • @zerkms, because both seem to be asking: *"Hey, is this an object?"* Of course, this behavior has a rigorous explanation, but JS types are generally confusing ("Is that a number? No, that is a *Number*. Huge difference."), and this one such case. – Paul Draper Nov 16 '14 at 21:42
  • @Paul Draper: "because both seem to be asking" --- well, it's not true. In JS objects don't have common root. If you want to know the symbol type - you use `typeof`. If you want to check if some object has another object in its prototype chain - you use `instanceof`. They are not interchangeable. – zerkms Nov 16 '14 at 21:42
  • @zerkms, they both *seem* to be asking. (Of course, that is my opinion about a *perception*, so it's hard to prove myself right or wrong.) – Paul Draper Nov 16 '14 at 21:43
  • @Paul Draper: that's the difference between natural and programming languages. When in doubts - one must check specification, not expect and assume based on their feelings. – zerkms Nov 16 '14 at 21:44
  • @zerkms, right, or in other words, the difference between *seem*, and *is*. (It applies to many other statements about a programming language. Saying an aspect of a language is "confusing" would likewise not be found in spec. It is a practical description of human comprehension, and still a valid observation.) – Paul Draper Nov 16 '14 at 21:45
  • @Paul Draper: then I agree. If one reads programming language code based on feelings not knowledge, then any programming language is confusing. `a = a + 1;` <--- how silly is it, isn't it? Math taught us it may never be true! JS is sooooo confusing :-D – zerkms Nov 16 '14 at 21:47
  • @zerkms: I totally agree. And there are [programming languages where `a = a + 1` is indeed invalid](http://stackoverflow.com/a/21554398/1048572) :-) – Bergi Nov 16 '14 at 21:48
  • @zerkms, thus Pascal's := , to avoid confusion, though C did not follow suit. – Paul Draper Nov 16 '14 at 21:49
  • @Bergi: and even more - there are languages, where there is no assignment operator :-) – zerkms Nov 16 '14 at 21:49

2 Answers2

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 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? – james emanon Nov 16 '14 at 22:06
  • 1
    @jamesemanon: No(t yet). But you can browse [my SO answers on the topic of course](http://stackoverflow.com/search?tab=votes&q=user%3a1048572%20is%3aanswer%20votes%3a4%20.prototype) :-) – Bergi Nov 16 '14 at 22:51
  • @jamesemanon: To be honest, I've never heard the term [OLOO](https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch6.md#delegation-theory) 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 Nov 16 '14 at 23:01
  • @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. – Bhojendra Rauniyar Feb 01 '15 at 00:49
  • @BhojendraNepal: No, it's really what I mean. `a` is the instance, `b` is the constructor. – Bergi Feb 01 '15 at 13:10
0

Referencing MDN:

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

Etheryte
  • 24,589
  • 11
  • 71
  • 116