4
 console.log(Function instanceof Object);//true
 console.log(Object instanceof Function);//true

This code is taken from article : https://github.com/stevekwan/experiments/blob/master/javascript/object-vs-function.html

I guess I understand what is prototypal inheritance and how Js object model works, but this circular links between two basic hmm.. probably constructors(which is functions which is objects .. and all objects are instance of Object constructor...) Function and Object just blow my mind.

In class-oriented languages i can imagine that there is some basic class Object, and every class i make is automatically inherits from it.

For me there is time consistency - class Object appears at first, then everything else i wrote appears at second etc. I don't really understand how two constructors magically appear at the same time and are instances of each other.

3 Answers 3

5

Note that Function and Object are both global symbols that refer to functions. So both of those functions are instances of both Object and Function, because they inherit from both prototypes.

It's less confusing if you do this:

var a = Function, b = Object;

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

Those functions have those properties just like any other function does:

var c = function() {};

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

On the left-hand side of the instanceof operator, all that matters is the nature of the object involved.

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

2 Comments

Pointy, thanks for reply. "Note that Function and Object are both global symbols that refer to functions. So both of those functions are instances of both Object and Function, because they inherit from both prototypes." How Object can be instance of itself?
@User15 The symbol Object refers to the constructor function for Objects. People usually don't write x = new Object(), but you can do that if you want. So that constructor is a function. As such, it's an object, just like every other function in JavaScript. All functions are objects, including those global constructors for the special object types. Don't get hung up on how it looks odd - remember that the left-hand side of typeof is evaluated to its basic value, and then the type is inspected. It doesn't matter that it's called "Object".
3

There is no circularity. There can't be cycles in [[Prototype]] chains.

Function instanceof Object means that the [[Prototype]] chain of Function contains Object.prototype.

Object instanceof Function means that the [[Prototype]] chain of Object includes Function.prototype.

It's not circular because Object is not in the [[Prototype]] chain of Object.prototype and Function is not in the [[Prototype]] chain of Function.prototype.

The [[Prototype]] chains are

╭╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╮
╎ Function ━┓       /* Function instanceof Object */         ╎
╰╌╌╌╌╌╌╌╌╌╌╌┃╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╮                     ╎
╭╌╌╌╌╌╌╌╌╌╌╌┃╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╮ ╎                     ╎
╎           ┣━━❯ Function.prototype ━━━━━❯ Object.prototype ━━━❯ null
╎           ┃                        ╎ ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
╎           ┃                        ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╮
╎ Object ━━━┛       /* Object instanceof Function */         ╎
╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯

1 Comment

Awesome drawing, +1! You could drive your point home by using code to iterate the __proto__ chain of each object.
1

x instanceof y is true if x.__proto__.__proto__... = y.prototype.

Function is the only built-object for which __proto__ (=its own prototype) and prototype (=prototype of its 'children') are equal (see pic). So

 Function instanceof Object

holds because

 Function.__proto__.__proto__ === Object.prototype

and

 Object instanceof Function

holds because

 Object.__proto__ === Function.prototype

enter image description here

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.