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.
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.
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 */ ╎
╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With