Is a function an instance of an Object, and an Object an instance of a function?

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

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

I suppose I understand what prototype inheritance is and how the Js object model works, but these are circular connections between two basic hmm .. possibly constructors (which are functions that are objects .. and all objects are an instance of Object constructor ...) Function and the object just blew my mind.

In class-oriented languages, I can imagine that there is some base class Object, and every class I make is automatically inherited from it.

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

+4
source share
3 answers

Please note that Functionand Objectare global symbols that refer to functions. Thus, both of these functions are instances of both Object, and Functionbecause they inherit from both prototypes.

This is 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);

These functions have properties like any other function:

var c = function() {};

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

On the left side of the operator, instanceofall that matters is the nature of the object.

+5

. [[Prototype]] .

Function instanceof Object , [[]] Function Object.prototype.

Object instanceof Function , [[Prototype]] Object Function.prototype.

, Object [[Prototype]] Object.prototype Function [[Prototype]] Function.prototype.

[[Prototype]]

โ•ญโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•ฎ
โ•Ž Function โ”โ”“       /* Function instanceof Object */         โ•Ž
โ•ฐโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ”ƒโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•ฎ                     โ•Ž
โ•ญโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ”ƒโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•ฎ โ•Ž                     โ•Ž
โ•Ž           โ”ฃโ”โ”โฏ Function.prototype โ”โ”โ”โ”โ”โฏ Object.prototype โ”โ”โ”โฏ null
โ•Ž           โ”ƒ                        โ•Ž โ•ฐโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•ฏ
โ•Ž           โ”ƒ                        โ•ฐโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•ฎ
โ•Ž Object โ”โ”โ”โ”›       /* Object instanceof Function */         โ•Ž
โ•ฐโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•Œโ•ฏ
+3

x instanceof ytrue if x.__proto__.__proto__... = y.prototype.

Function- the only built-in object for which __proto__(= its own prototype) and prototype(= prototype of its "children") are equal (see. Fig.). So

 Function instanceof Object

performed because

 Function.__proto__.__proto__ === Object.prototype

and

 Object instanceof Function

performed because

 Object.__proto__ === Function.prototype

enter image description here

+1
source

Source: https://habr.com/ru/post/1660941/


All Articles