0

Excuse my noobidity, but I do not really understand the following:

Function instanceof Object

It returns true. OK. I understand that... so Function is in fact an object. Rather it is the constructor for any function. So when you create a function you create an object which has a prototype that is itself an object and that prototype holds the constructor of the function. And it inherits from Object because any object is created via Object constructor.

But why does

Object instanceof Function

return true as well ?

2
  • 1
    Object is the constructor for objects, it's a function. Commented Dec 24, 2014 at 7:30
  • This answer actually explains why this happens. Commented Jun 9, 2015 at 4:12

2 Answers 2

4
Object instanceof Function

It returns true because Object is an instance of Function. It's the object constructor. Call it as

Object({})

Looks like a function, doesn't it, and sure enough it is one. Has a call method and everything:

Object.call(0, {})

In the same vein,

Number instanceof Function // true
String instanceof Function // true

The confusion may arise from the natural tendency to think that Object instanceof Function means that objects are instances of functions. No, objects are not instances of functions, and Object instanceof Function does not mean that, it means that a very particular value whose name is Object is an instance of Function. Number instanceof Function does not mean that numbers are instances of functions, it means that the particular object named Number, the constructor for the number type, is an instance of a function, because is is:

Number(1)

It may be clearer to note that

String instanceof String // false
Date   instanceof Date   // false

because the String constructor is not a string--it's a function.

However,

Function instanceof Function // true

You may think that's obvious, but it's not because "functions are functions"--it's because the Function object is a constructor function (for functions).

By the same token

Object instanceof Object // true

may also seem obvious, but it's not because "objects are objects", but because the value named Object is a function which in turn are objects.

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

3 Comments

So..when you create a function you actually create an object function. And that object function has a prototype. And that prototype is an object. And it holds the constructor.And all the data you want to initialize.But now I understand that the Function function is a weirdo. Because it creates it's own prototype (as any other function does, so it makes sense), but it inherits from that prototype. And any other function including the big ones:Object,Array,Number,String inherit from this one. On the other hand, those prototypes being objects they inherit from Object. Right? Thanks.
That is why Object has all the methods a function does. And that is why any prototype has whatever an object has. So is it safe to say that all constructors are found in the prototype property of any function object ? And all constructors point back to their respective function objects.
See this answer for an explanation to OP's question.
0

All the in built constructors like Object, Array, String etc are Functions. So Object instanceof Function must hold true.

Now if you try obj = new Object();

then

obj instanceof Function

...it will be false, because what you now have is not a function or a constructor but an object.

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.