12

console.log(Object instanceof Object);
console.log(Object instanceof Function);
console.log(Function instanceof Object);
console.log(Function instanceof Function);

so if Function is an Object and the Object is a Function how come Function === Object and Function == Object are false?

I do understand that checking the instance of an object is not the same as comparison. So the question here is the fuzziness in the case where if two objects (which are actually types) are instances of each other, shouldn't the types be the same?

Note: Object is not an instance of a Number or an Array just an instance of Function.

dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Jan 4, 2010 at 23:49
1
  • Why would the types be the same? The instanceof relation is not antisymmetric. Hell, it’s not even reflexive: Number instanceof Number is false. So why would you expect it to behave like an ordering relation? Commented Jan 1, 2025 at 9:09

6 Answers 6

18

From JavaScript Prototypal Inheritance:

Quite everything, in JavaScript, inherits from Object. We could say that Object is the super class, or better, the super constructor, of every variable and that everything is an instanceof Object. The Object constructor is a Function, but a Function is an instanceof Object. This means that these assertions are always true:

(Object instanceof Function) === (Function instanceof Object)

Above example is true because Object is a constructor, and a constructor in JavaScript is always a Function. At the same time, every Function has its own prototype, and a prototype always starts its inheritance from Object.prototype. The Function constructor, is a Function itself, and the function prototype is a function(){};

(Function.prototype instanceof Object) === (function(){} instanceof Object)

user123444555621
154k27 gold badges118 silver badges126 bronze badges
answered Jan 5, 2010 at 0:19
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for saying the same thing I said but in a much more concise and easy-to-understand way. xD
Quite everything, in JavaScript, inherits from Object and then ES5 came: var notObject = Object.create(null); notObject instanceof Object; //false
That is the most confusing explanation I have ever seen.
@Derek朕會功夫 True. And of course, even before ES5 the assertion was wrong. Clearly, every prototype chain is finite, thus Object.prototype instanceof Object === false
5

Everything is an Object in JavaScript because JavaScript is an object-oriented language. Function is an instance of Object because everything is an instance of Object. Simple enough. However, objects that initialize other objects (constructors) are also Functions in JavaScript, so it would make sense for Object to also be a Function.

Think about this:

var obj = new Object();

Object in this case is used as a Function, is it not? So while, in theory, Object should be the lowest-level object in the language, JavaScript cannot function without Functions (pun!), so you need both to be at the same level. Object needs to be an instance of Function because it's a constructor and it needs to create more instances of itself.

function FooBar() {}

The FooBar class above is an instance of both Object and Function, because it's both. The same logic applies to the built-in Object and Function objects; they're instances of both.

Phew, confusing. Did that make any sense?

answered Jan 5, 2010 at 0:15

4 Comments

Makes sense to me, but as you can probably see I had just as much trouble (unsuccessfully) trying to explain the same thing :-P
Haha yeah, it's not easy to explain!
It certainly makes sense given that it is Javascript, in no other language that I know of do you instantiate a function (of type Function), you only invoke it. Appreciate your effort in trying to answer the right question.
there are many Functional Programming languages that can have functions that generate functions LISP, LOGO, ... even BASIC! (the technique involves creating a string and embedding it in the BASIC program as a line - the embedded string would define a function)
4

I think this is more due to the unique way in which objects are defined. You don't define a type in javascript, you define a constructor. But you also do not define the constructor as a constructor, it's simply a function.

You can then refer to the types by the name of their constructor....which is just a function.

function Tiger(){ //function, or object?
}
function Apple(){ //function, or object?
}

Both could be objects, or perhaps just functions. Only the way you use them will determine that. So it kind of makes sense that at a low level, objects are functions and functions are objects, but there is still a difference, right?

Sasha Chedygov
132k27 gold badges107 silver badges117 bronze badges
answered Jan 5, 2010 at 0:13

2 Comments

Regarding your comment method or object, it is definitely not a method unless it is called with some scope.
Ah, yes, is this a terminology clash here - would "//FUNCTION, or Object?" be a better comment?
1

There is no such thing as classes in Javascript. The instanceof operator is called on functions.

Object is a constructor function for "Object objects" (yes, this is the official term), and Function is the constructor function for "Function objects".

So, when you call Function instanceof Object, it returns true because Function is a function, and thus an object, etc. This does not mean that the types are the same, because Object and Function have different prototypes:

Object.prototype
 ^
 | inherits from
 | instance
Function.prototype <------- Object, Function
answered Sep 8, 2014 at 4:59

4 Comments

Nice copy and pasting from your last answer.
...since the question was closed as a duplicate
I deleted the other answer, to avoid duplicate content :)
But what is not shown in diagram is Object.prototype.constructor === Object but Object instance of Function.prototype. This whole thing has no logic no matter how any one tries to explain.
-1

Suppose:

<---- : links to (via .__proto__)

<-- : has inherited property of (via .)

Then:

Function.prototype <---- {Object, Function}

Object <---- Function.prototype

Object.prototype <-- Object

console.log(Object.prototype.constructor === Object); // true!

Object.prototype does not link to anything; it doesn't have __proto__ property.

answered Dec 10, 2015 at 12:17

Comments

-2

instanceof operator indicates if the first argument is of the given type. That is saying

 A instanceof B

returns true if A is an instance of the type B.

The == and === operators are comparison operators on the other hand. They compare values for equality.

For instance, you can say jack instanceof Boy is true but would you say that jack == boy? No.

answered Jan 4, 2010 at 23:56

1 Comment

The OP's question is more about how Object and Function can be instances of each other and not be the same thing.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.