Home > Article > Web Front-end > Sharing of design ideas of Javascript inheritance mechanism_javascript skills
我一直很难理解Javascript语言的继承机制。
它没有"子类"和"父类"的概念,也没有"类"(class)和"实例"(instance)的区分,全靠一种很奇特的"原型链"(prototype chain)模式,来实现继承。
我花了很多时间,学习这个部分,还做了很多笔记。但是都属于强行记忆,无法从根本上理解。
直到昨天,我读到法国程序员Vjeux的解释,才恍然大悟,完全明白了Javascript为什么这样设计。
下面,我尝试用自己的语言,来解释它的设计思想。彻底说明白prototype对象到底是怎么回事。其实根本就没那么复杂,真相非常简单。
一、从古代说起
要理解Javascript的设计思想,必须从它的诞生说起。
1994年,网景公司(Netscape)发布了Navigator浏览器0.9版。这是历史上第一个比较成熟的网络浏览器,轰动一时。但是,这个版本的浏览器只能用来浏览,不具备与访问者互动的能力。比如,如果网页上有一栏"用户名"要求填写,浏览器就无法判断访问者是否真的填写了,只有让服务器端判断。如果没有填写,服务器端就返回错误,要求用户重新填写,这太浪费时间和服务器资源了。
因此,网景公司急需一种网页脚本语言,使得浏览器可以与网页互动。工程师Brendan Eich负责开发这种新语言。他觉得,没必要设计得很复杂,这种语言只要能够完成一些简单操作就够了,比如判断用户有没有填写表单。
1994年正是面向对象编程(object-oriented programming)最兴盛的时期,C++是当时最流行的语言,而Java语言的1.0版即将于第二年推出,Sun公司正在大肆造势。
Brendan Eich无疑受到了影响,Javascript里面所有的数据类型都是对象(object),这一点与Java非常相似。但是,他随即就遇到了一个难题,到底要不要设计"继承"机制呢?
二、Brendan Eich的选择
如果真的是一种简易的脚本语言,其实不需要有"继承"机制。但是,Javascript里面都是对象,必须有一种机制,将所有对象联系起来。所以,Brendan Eich最后还是设计了"继承"。
但是,他不打算引入"类"(class)的概念,因为一旦有了"类",Javascript就是一种完整的面向对象编程语言了,这好像有点太正式了,而且增加了初学者的入门难度。
他考虑到,C++和Java语言都使用new命令,生成实例。
C++的写法是:
ClassName *object = new ClassName(param);
Java的写法是:
Foo foo = new Foo();
因此,他就把new命令引入了Javascript,用来从原型对象生成一个实例对象。但是,Javascript没有"类",怎么来表示原型对象呢?
这时,他想到C++和Java使用new命令时,都会调用"类"的构造函数(constructor)。他就做了一个简化的设计,在Javascript语言中,new命令后面跟的不是类,而是构造函数。
举例来说,现在有一个叫做DOG的构造函数,表示狗对象的原型。
function DOG(name){
this.name = name;
}
对这个构造函数使用new,就会生成一个狗对象的实例。
var dogA = new DOG('大毛');
alert(dogA.name); // 大毛
注意构造函数中的this关键字,它就代表了新创建的实例对象。
三、new运算符的缺点
用构造函数生成实例对象,有一个缺点,那就是无法共享属性和方法。
比如,在DOG对象的构造函数中,设置一个实例对象的共有属性species。
function DOG(name){
this.name = name;
this.species = '犬科';
}
然后,生成两个实例对象:
var dogA = new DOG('大毛');
var dogB = new DOG('二毛');
The species attributes of these two objects are independent. Modifying one of them will not affect the other.
dogA.species = 'cat';
alert(dogB.species); // Display "canine", not affected by dogA
Each instance object has its own copy of properties and methods. This not only fails to achieve data sharing, but is also a huge waste of resources.
4. Introduction of prototype attribute
With this in mind, Brendan Eich decided to set a prototype attribute for the constructor.
This attribute contains an object (hereinafter referred to as "prototype object"). All properties and methods that need to be shared by instance objects are placed in this object; those properties and methods that do not need to be shared are placed in the constructor.
Once the instance object is created, it will automatically reference the properties and methods of the prototype object. In other words, the properties and methods of instance objects are divided into two types, one is local and the other is reference.
Still taking the DOG constructor as an example, now rewrite it with the prototype attribute:
function DOG(name){
this.name = name;
}
DOG.prototype = { species : 'Canidae' };
var dogA = new DOG('Big Hair');var dogB = new DOG('二毛');
alert(dogA.species); // Caninealert(dogB.species); // Canine
Now, the species attribute is placed in the prototype object and is shared by the two instance objects. As long as the prototype object is modified, it will affect both instance objects.
DOG.prototype.species = 'Feline';
alert(dogA.species); // Cats
alert(dogB.species); // Cats
5. Summary
Since all instance objects share the same prototype object, from the outside world, the prototype object seems to be the prototype of the instance object, and the instance object seems to "inherit" the prototype object.
This is the design idea of Javascript inheritance mechanism. I don’t know if I made it clear. For the specific application method of inheritance mechanism, you can refer to the series of articles I wrote