It looks like you are getting types, functions and objects confused.
Short answer:
In Javascript functions are objects. Objects are not functions, but functions exist that create and/or return objects.
Detailed answer with examples:
You are correct that functions are objects. Object
is a function and so is Function
as shown in the console output below.
But when you say that Object
is a function, you are actually talking about the Object()
function, and not the type object
.
// Object is a function
> Object
function Object() { [native code] }
// Function is a function
> Function
function Function() { [native code] }
// The type of Object is function
> typeof(Object)
"function"
// The type of the result of invoking the Object() function (AKA constructor, since using "new" keyword) is a new object
> typeof(new Object())
"object"
> new Object() instanceof Object
true
> new Function() instanceof Function
true
// note the difference
> new Object() instanceof Function
false
> new Function() instanceof Object
true
In your example in some cases you are actually invoking the function and looking at the result of the function, and not the function itself. For example, typeof(String) === "function"
but typeof(new String()) === "object"
(a "String" object in this case).
When you invoked these functions with the new
keyword you got a new object whose class is the name of the function you invoked. The reason that new Function() instanceof Object === true
is that Object
is the base class for any object constructed in this way. Object
is the name of the base class, and the type of the instance is "object"
(that is, the object created from the class, like cookies from a cookie cutter).