4

I'm new to JavaScript coming from a Java background. I have difficulty understanding the following behavior.

console.log(Object.constructor.name); // prints Function.

console.log(Object instanceof Function); // prints true since Object's constructor is Function.

So that means Object is an instance of Function.

console.log(Function instanceof Object); // prints true

How can Function be an instance of Object if Object is an instance of Function?

I ran the code in the latest Google chrome browser.

user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • Because they are both built–in [*Objects*](http://ecma-international.org/ecma-262/5.1/#sec-15.2) that are also [*constructors (Functions)*](http://ecma-international.org/ecma-262/5.1/#sec-15.3) and that's how ECMA-262 specifies their relationship. The environment is just established that way, one isn't built from the other. – RobG Sep 07 '14 at 23:06
  • 1
    `prints true since Object's constructor is Function` That is not accurate. It prints true because Object's prototype is inherited from Function's prototype: `Object._proto__ === Function.__proto__ //true` They are both functions. – Derek 朕會功夫 Sep 07 '14 at 23:11
  • 1
    Why do you want to find logic where it isn't? Javascript has a very bad type checking system, you can use typeof and instanceof for the same purposes: checking primitive types... Sometimes they are inconsistent, for example `1 instanceof Number` returns false, etc... – inf3rno Sep 07 '14 at 23:16
  • @inf3rno - Well that's not inconsistent because `1` is not an Object, therefore it isn't an instance of `Number`. – Derek 朕會功夫 Sep 07 '14 at 23:17
  • @Derek朕會功夫 Yes I know this interpretation, but it is still awkward... – inf3rno Sep 07 '14 at 23:18
  • var a = 1; a.constructor.name prints Number. But a instanceof Number prints false. – user2054558 Sep 07 '14 at 23:20
  • @user2054558 yes because it converts the 1 to a Number instance by these cases... – inf3rno Sep 07 '14 at 23:21
  • @user2054558 - The `a` inside `console.log` is wrapped in a `Number` wrapper automatically. – Derek 朕會功夫 Sep 07 '14 at 23:21
  • @Derek朕會功夫 now why can't it convert the 1 to a Number instance by instanceof too, and we could forget the typeof ????? – inf3rno Sep 07 '14 at 23:22
  • @inf3rno - JS only wraps primitive with a wrapper object when you try to access its "properties". – Derek 朕會功夫 Sep 07 '14 at 23:28
  • @Derek朕會功夫 I use js for 15+ years, please, I already knew that... I asked only what is simpler and more consistent from the following 2 options? a.) `1 instanceof Number` -> true and `typeof` does not exist. b.) `1 instanceof Number` -> false and `typeof(1)` -> `"number"`. I don't think we really need 2 inconsistent operators to make a simple type check a nightmare... – inf3rno Sep 07 '14 at 23:33
  • @inf3rno Ok I thought you were asking me a question. (Look at all those ???s) – Derek 朕會功夫 Sep 07 '14 at 23:38
  • 1
    @Derek朕會功夫 It's just the sign of pain, because js is not perfect, not a question :D – inf3rno Sep 07 '14 at 23:44
  • 1
    I agree with @inf3rno as there is no logic here as on one hand Object [[Prototype]] is Function.prototype and on other hand [[Prototype]] of Function.prototype is Object.prototype and his constructor is Object. I mean what nonsense is this. People try to come with explanation which are soothing but nothing is concrete enough to make this sound logical. and the bad part is this is not the only area in JS that sounds illogical. – Number945 Jan 24 '17 at 20:32

1 Answers1

2

Both Object and Function are constructors, so they are functions.

The expression Object instanceof Function returns true because Object is a function, so it's an instance of the type Function.

The expression Function instanceof Object returns true because Function is a function, which is of the type Function which inherits from the type Object.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005