0

In other words what was first? An egg or a hen? I can't understand how JavaScript could be implemented since I read this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#Properties_and_Methods_of_Function .

As you see in console:

>Object instanceof Function
true
>new Function() instanceof Object
true

Seems like a fatal loop.

And why all of these:

typeof new Object()
typeof new Array()
typeof new Date()
typeof new RegExp()
typeof new Number()
typeof new Boolean()
typeof new String()

return "object", but this:

typeof new Function()

returns "function"

Seems like what... Is object derived from function? I don't think so because:

>new Function() instanceof Object
true
>new Object() instanceof Function
false

So not...

3
  • 1
    Object is a function which is also an object, and new creates an object, which can be a function. Commented Oct 28, 2014 at 1:29
  • 1
    possible duplicate of Why in JavaScript both "Object instanceof Function" and "Function instanceof Object" return true? and Object and Function are quite confusing Commented Oct 28, 2014 at 2:26
  • object is a type; Object is a function and an object (all functions are objects); Object is a class that can construct objects of type object that are instances of the Object class. All other classes inherit from the base class Object. So instances of new Function() are functions, which are objects that are instances of the Function class whose base class is Object. So a function (or an object with the type function) is both a Function and an Object. Clear as mud, right? Commented Oct 28, 2014 at 2:57

1 Answer 1

2

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).

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.