Home > Web Front-end > JS Tutorial > body text

A detailed introduction to the birth of everything in the JavaScript world

黄舟
Release: 2017-03-03 15:10:08
Original
1127 people have browsed it

1. Making something out of nothing

At first, there was nothing.

The Creator said: Nothing is a thing in itself, so there is null:

Now we have to create something. But what to do if there are no raw materials?

A voice said: Isn’t there null?

Another voice said: But null means nothing.

The Creator said: Then make something out of nothing!

So:

The No. 1 object in JavaScript is generated. You might as well call it No. 1.

This No. 1 object is incredible, it is the true ancestor of all things. It has properties that all objects have.

What is __proto__? It means "birth", or inheritance.

2. Machine for making objects

Since we already have an object, the rest is easy to handle, because one thing is two, two is three, and three is everything.

But the Creator is very lazy, he doesn’t want to create objects one by one with his own hands. So he built a machine that could make objects:

He gave the machine a name: Object.

This machine cannot create objects out of thin air. It needs a template object to create objects according to this template object. Naturally, it uses the only No. 1 object as a template. The prototype in the picture represents the template object of the machine.

How to start the machine? Through the new command. You shout "new!" to the machine, and the object is created.

The emergence of machines has realized the batch automated production of objects and liberated the hands of the Creator. Then the Creator went off to do other things.

It would be too stupid if the machine just copied the exact same object mechanically according to the template.

On the basis of inheriting the traits of their parents, human offspring can develop traits that their parents do not have. Similarly, when a machine manufactures an object, in addition to inheriting the properties of the template object, it can also add new properties. This makes the JavaScript world increasingly diverse.

For example, one day the Object machine creates an object. It has a special attribute called flag, and the attribute value is 10. The graphic representation is like this:

Written in code is:

var obj = new Object({ flag: 10 });
Copy after login

The vigorous movement of creation has begun...

3. Update Machines that make many objects

As the days passed, the Creator came to inspect the work. He was very happy to see that Object had created many objects.

At the same time, he also discovered that according to the principle of "birds of a feather flock together", these objects can be divided into many categories. The clever creator thought, why don't I build a few more machines, and let each machine be responsible for manufacturing a certain type of object? So he built several machines and gave them names. They are:

String: used to create an object representing a piece of text.
Number: Used to create an object representing a number.
Boolean: used to create objects that represent right and wrong.
Array: used to create ordered queue objects.
Date: Used to create an object representing a date.
Error: Used to create an object representing an error.
...

Multiple machines are started at the same time, each performing its own duties, and the creation movement has entered a new stage...

The creator began to think again: Although the machine is used to create Object, but the machine itself is actually a special object. Now that there are so many machines, I have to summarize their common characteristics and incorporate them into the object system.

So, the Creator created a No. 2 object based on the No. 1 object, using it to represent the common characteristics of all machines. In other words, use it as the prototype object for all machines.

(Note: __proto__ is too troublesome to write, we will use [p] instead later)

Of course, like Object, these machines They also need to each have a template object, that is, the object pointed to by their prototype attribute. Obviously their template objects should inherit from the No. 1 object, that is

这张图显示了JavaScript世界中那些最基本的机器本身的原型链,以及它们的模板对象的原型链。不过看起来太复杂了,所以后面我们就不再把它们完整地画出来了。

四. 制造机器的机器

造物主高兴地想:这下可好了,我造出了Object机器,实现了对象制造的自动化。然后又造出了String、Number等机器,实现了特定类别的对象制造的自动化。但是,为啥总感觉似乎还缺点什么呢?

对啦,还缺少一台制造机器的机器啊!

很快,万能的造物主就把它造了出来,并把它命名为Function。有了Function机器后,就可以实现自动化地制造机器了。
让我们来观察一下Function:

首先,Function是一台机器,所以它的原型对象也是No. 2对象。
其次,Function又是一台制造机器的机器,所以它的模板对象也是No. 2对象。
所以我们得到了Function的一个非常特别的性质:

Function.__proto__ === Function.prototype
Copy after login

哇,太奇妙了!

不要奇怪,这个性质不过是”Function是一台制造机器的机器“这个事实的必然结果。

从这张图中,我们发现:所有的函数(包括Function)的原型都是No. 2对象,而同时Function.prototype也是No. 2对象。这说明了:

从逻辑上,我们可以认为所有机器(包括Function自己)都是由Function制造出来的。

同时,如果再仔细瞧瞧,你会发现:

Object作为一个机器可以看做是有由Function制造出来的,而Function作为一个对象可以看做是由Object制造出来的。

这就是JavaScript世界的“鸡生蛋,蛋生鸡”问题。那么到底是谁生了谁呢?Whatever!

五. 让世界动起来

就像前面所说,机器用来制造某一类对象。正因如此,机器可以作为这类对象的标志,即面向对象语言中类(class)的概念。所以机器又被称为构造函数。在ES6引入class关键字之前,我们常常把构造函数叫做类。

然而,除了作为构造函数来制造对象外,函数通常还有另一个功能:做一件事情。正是有了这个功能,JavaScript的世界才由静变动,变得生机勃勃。

比如说,我们现在用Function机器制造了鸟类(即用来造鸟的机器):

function Bird(color) {
    this.color = color;
}
Copy after login

然后,对着造鸟机说:“new!”,于是造鸟机发动起来,制造一个红色的鸟:

var redBird = new Bird('#FF0000');
Copy after login

如果现在我们想让鸟飞起来,该怎么办呢?我们需要再次用Function制造出一台机器,不过这台机器不是用来制造对象的,而是用来做事儿的,即“让鸟飞起来”这件事情:

// 这是一台通过晃动鸟的翅膀,让鸟飞起来的简陋的机器。
function makeBirdFly(bird) {
    shakeBirdWing(bird);
}
Copy after login

我们知道,让一台制造对象的机器发动,只需要对它喊“new”即可;那么怎样让一台做事情的机器发动呢?更简单,对它咳嗽一声就行了。咳咳咳,

makeBirdFly(redBird);
Copy after login

于是红鸟飞了起来,世界充满了生机。

从上面的Bird和makeBirdFly的定义可以看出:实际上,制造对象的机器和做事情的机器没什么明显区别,不同的只是它们的使用方式。在两种情况下,它们分别被叫做构造函数和普通函数。

说明1:function xxx语法可以看成new Function的等价形式。
说明2:用户自定义的函数通常既可以作为普通函数使用,又可以作为构造函数来制造对象。ES6新增的class语法定义的函数只能作为构造函数,ES6新增的=>语法定义的箭头函数只能作为普通函数。

六. 让世界立体起来

造物主对目前的世界还是不太满意,因为几乎所有的机器的模板对象都是No. 2,这使得JavaScript世界看起来有点扁。

于是造物主再次研究世界万物的分类问题。他发现有些对象会动、还会吃东西,于是把它们叫做动物,然后造了一台Animal机器来制造它们。他进一步发现,即使都是动物,也还是可以进一步分类,比如有些会飞、有些会游,他分别把它们叫做鸟类、鱼类。于是他想,我何不单独造几台机器,专门用来制造某一类动物呢。于是它造出了Bird、Fish等机器。

接下来,在选择这些机器的模板对象时碰到一个问题:如果还像之前那样直接复制一个No. 1对象作为Bird、Fish的模板,那么结果就是这样的:

这样可不好。首先没体现出鸟类、鱼类跟动物的关系,其次它们的模板对象存了重复的东西,这可是一种浪费啊。怎么办呢?简单,让Bird和Fish的模板对象继承自Animal的模板对象就好了。就是说

Bird.prototype.__proto__ === Animal.prototype
Fish.prototype.__proto__ === Animal.prototype
Copy after login

于是:

用同样的方法,造物主造出了一个立体得多的JavaScript世界。

然而这样还不够。虽然那些纯对象现在充满了层次感,但是那些机器对象之间的关系还是扁平的:

那又该怎么办呢?其实用类似的办法就行了:

为了更方便地做到这一点,造物主发明了class关键字。

七. 世界最终的样子

经过一番折腾,JavaScript世界发生了大变化。变得丰富多彩,同时变得很复杂。用一张图再也没法画出它的全貌,只能画出冰山一角:

JavaScript的世界还在不断进化中……

 以上就是JavaScript世界万物诞生记的详细介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template