“This is the seventh day of my participation in the First Challenge 2022.

Function and Object

There are two classes in JavaScript that have a subtle relationship: Function and Object. Why do they have a subtle relationship? Function instanceof Object and Object instanceof Function are both true

instanceof

The instanceof operator tests whether the constructor’s Prototype property appears anywhere in the object’s prototype chain

Function

The Function constructor creates a new Function object. In JavaScript, every Function is actually a Function object.

Object

The Object constructor creates an Object wrapper. The Object constructor creates an Object wrapper for the given value. If the given value is null or undefined, an empty object is created and returned; otherwise, an object of the given type is returned. When called as a non-constructor, Object is equivalent to new Object().

Object.prototype.constructor

Returns a reference to the Object constructor that creates the instance Object. Note that the value of this attribute is a reference to the function itself, not a string containing the name of the function. For primitive types such as 1, true, and “test”, this value is only readable.

That is to say, their prototype chain contains each other, because the prototype chain indirectly describes the inheritance relationship between objects.

If Object is on the prototype chain of Function, then Object can be considered an instance of Function.

Object is a class or constructor, so it makes sense to say that it is an instance of a function

But Function instanceof Object is also true, which means that Object is on the prototype chain of Function.

There seems to be nothing wrong with everything being an object

But which of them are examples of whom? Who came first? This is undoubtedly a chicken-and-egg problem.

Function is not an instance of Object, but an instance of Function itself

Function.constructor === Object //false
Function.constructor === Function //true
Object.constructor === Function //true
Copy the code

But what is the truth?

The origin of

Pan Gu created the world, js does not have Object, but Object. Prototype.

Everything in JS (with the exception of the original values) inherits from Object, with the exception of object.prototype.

Object instanceof Object;  //true
Object.prototype instanceof Object; // false 
Copy the code

JS has no classes. What are we talking about when we say that a variable is an instance of another variable? (What does Instanceof compare?)

With Object.create we have the following chestnuts:

var p = {test:1}
var obj = Object.create(p)
function A(){}
obj instanceof A  //false
A.prototype = p
obj instanceof A  //true 
Copy the code

When we say that an object inherits from Constructor, we are saying that Constructor. Prototype is found from the object’s prototype chain.

Function. Prototype = Function. Prototype = Function. Object constructor. Attach Object. Prototype to Object and Function. Prototype to Function.

For example, obj in chestnut is constructed from P, and after obj is constructed, P is attached to A. We say that OBj is of type A, but obj is constructed from a. prototype(that is, P).

Prototype: Function. Prototype: Object. Prototype: Function.

Object.getPrototypeOf(Object) = = =Function.prototype   / / are congruent
Object.getPrototypeOf(Function.prototype) === Object.prototype  / / are congruent
Copy the code

Prototype = Function. Prototype = Function.

The pseudo-code looks something like this, and the idea behind the CREATE meta-operation is to construct a new object using the given object as a prototype.

let ObjectPrototype = create( );   // C++ was invented
 
let FunctionPrototype = create( ObjectPrototype );   
//FunctionPrototype (later assigned to function. prototype) is of type Object
// Because its prototype is ObjectPrototype
 
let Function = create( FunctionPrototype );
 
Function.prototype = FunctionPrototype;
 // Function is a Function and an Object
Function. Prototype and Object. Prototype
 
Object =  create( FunctionPrototype ); 
 
Object.prototype = ObjectPrototype;
 //Object is a Function and an Object
Function. Prototype and object. prototype are attached to the prototype chain of Object objects
Copy the code

Of course, the construction of Function is far from that simple. Here we simply explain the complicated relationship between Object and Function.