The idea of this article is mainly from JavaScript, the birth of all things in the world. Finally, I will sort out some classic questions on the Internet with some understanding. I will bring these links to the next article
We’ve probably heard the saying in many places that JavaScript is everything. This isn’t exactly accurate because we know undefined isn’t an object, but it’s true that most data in JavaScript is an object, so the question is, in what order should objects be generated? For Object, it is an instanceof Function, because Object instanceof Function // true; For Function, it is an instanceof Object, because Function instanceof Object // true, which comes first? This article introduces you to the generation process of JavaScript objects
Initial chaos
We can think of the JavaScript world as a self-contained factory, which can be interpreted as our V8 engine. At first there was nothing in the factory, but the factory thought nothing was a thing in itself, so null appeared
null
JavaScript
JavaScript
Object.prototype
JavaScript
Object.prototype.__proto__ === null
A machine that makes objects
Well, since templates already exist, can’t factories just manually create one from this one? But this factory was too inefficient, so the factory had an idea and built a machine to create objects (objects) out of the template of the Object. Prototype element mentioned above
Object.prototype
new Object
new Object({}).__proto__ === Object.prototype
Machines that make different objects
Now the factory can create many of these items, but it’s too boring for the factory. The factory suddenly came up with the idea that we could build more machines (arrays, strings, etc., in JavaScript) to create different items, but the machine itself is an object. Now that I have so many machines, I want to distinguish this kind of machine object from the concrete object object THAT I create, so the factory creates the tianzi element based on tianzi element 1
The machine that makes the machine
Well, now the factory is rich in the world, different machine to create a different item, everybody happy with each other, after a period of time, the external world have a demand, I don’t want this item, you gave me to create a different, thought the factory, if so, every time themselves is not dead, so it creates a super bad things, The machine that creates the machine, which we call Function, because it is used to create the machine, so its template object is the tianzi 2 element; But it is also a machine, so the example of the Tian-2 object is
Function.__proto__ === Function.prototype
Copy the code
This is also the most famous chicken-and-egg problem in JavaScript
The question
Okay, now that the whole JavaScript world is working, there are a few questions about the creation process that I’ll leave to ponder
- Day number element
Object.prototype
How did you get throughnull
I created it Function.__proto__ === Function.prototype;
Function
constructionalprototype
Properties and__proto__
Attributes all point to the same stereotypeFunction
Object is aFunction
An instance created by the constructor
Classic image
Function
Object
- The red line:
new Object()
The template object of theObject.prototype
.Object
The prototype object of isObject.prototype
, its template object isFunction.prototype
.Object.prototype
The template object of thenull
new Object().__proto__ === Object.prototype;
Object.__proto__ === Function.prototype;
Object.prototype.__proto__ === nulll;
Copy the code
- Yellow line:
Function
Both the prototype object and the template object areFunction.prototype
.Function.prototype
The template object of theObject.prototype
Function.prototype === Function.__proto__;
Function.prototype.__proto__ === Object.prototype;
Copy the code
- Black lines:
new Foo()
The template object of theFoo.prototype
.Foo.prototype
The template object of theObject.prototype
.Foo
The template object of theFunction.prototype
new Foo().__proto__ === Foo.prototype;
Foo.prototype.__proto__ === Object.prototype;
Foo.__proto__ === Function.prototype
Copy the code
Refer to the article
- The birth of the JavaScript world