Note: The theory is too deep. I will not change it to ensure that all theories are correct. However, the actual code and theoretical conflicts have not been found after multiple tests. In case of any errors, please kindly advise!
Function
First, let's review the concept of a function object. A function is an object, and an object that represents a function is a function object. All Function objects are constructed by the Function object. That is to say, Function is the top-level constructor. It constructs all objects in the system, including user-defined objects, built-in system objects, and even including itself. This also indicates that the Function is self-initiated (the ability to construct itself ). This indirectly determines that the [[call] and [constructor] logics of the Function are the same.
Copy codeThe Code is as follows:
Function Foo (){};
Var foo = new Foo ();
// Constructor whose Foo is foo
Alert (foo instanceof Foo); // true
// But the Function is not the foo Constructor
Alert (foo instanceof Function); // false
// Constructor whose Function is Foo
Alert (Foo instanceof Function); // true
The code above explains the relationship between foo and its constructor Foo and the constructor Foo. (For details, refer to the memory relationship between Function and Object)
Object
For an Object that is the top-level Object, all objects will inherit the prototype of the Object, but you must also be clear that the Object is also a function Object, therefore, objects are constructed by functions. (There are not many theories about objects)
Function and Object
This is the focus of this article and is very important!
Copy codeThe Code is as follows:
Alert (Function instanceof Function); // true
Alert (Function instanceof Object); // true
Alert (Object instanceof Function); // true
Function Foo (){};
Var foo = new Foo ();
Alert (foo instanceof Foo); // true
Alert (foo instanceof Function); // false
Alert (foo instanceof Object); // true
Alert (Foo instanceof Function); // true
Alert (Foo instanceof Object); // true
Can you understand these answers? Congratulations! You have understood the nature of Javascript.
Let's take a look at the actual relationship between objects and functions:
Before you look at the diagram, read the two articles on the principles of function objects and instanceof. Otherwise, the memory diagram is hard to understand.
Here, I want to explain the memory graph: the construction process of the Function object is mentioned in the Function object article. In this article, the Function is self-lifting, therefore, the construction process of Function object Foo is the same as that of Function object Function. Therefore, we use '|' to separate the highlighted values in the graph to indicate that their construction process is the same. According to the instanceof theory and memory diagram, you can export the correct results of the preceding statements. Here we will not talk about it one by one. Let readers know it.
If you cannot understand this complex memory diagram, you can refer to the following illustration to help you understand it:
Note: The actual execution process of the Code is not exactly as described in this figure. That is to say, this figure is problematic (or incorrect). It cannot explain why Function instanceof Function is true. However, it is easy to understand the relationship between functions and objects.