Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object and Function are quite confusing

Tags:

javascript

Object instanceof Object
true
Object instanceof Function
true
Function instanceof Object
true
Function instanceof Function
true

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.

like image 865
Murali Avatar asked Jan 04 '10 23:01

Murali


3 Answers

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)

like image 122
Annie Avatar answered Nov 05 '22 17:11

Annie


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?

like image 27
Sasha Chedygov Avatar answered Nov 05 '22 18:11

Sasha Chedygov


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?

like image 4
Graza Avatar answered Nov 05 '22 18:11

Graza