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.

14
  • Because they are both built–in Objects that are also constructors (Functions) and that's how ECMA-262 specifies their relationship. The environment is just established that way, one isn't built from the other. Commented Sep 7, 2014 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. Commented Sep 7, 2014 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... Commented Sep 7, 2014 at 23:16
  • 1
    @Derek朕會功夫 It's just the sign of pain, because js is not perfect, not a question :D Commented Sep 7, 2014 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. Commented Jan 24, 2017 at 20:32

1 Answer 1

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.

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

2 Comments

Also, Function instanecof Function
and Object.__proto__ === Function.prototype

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.