js 原型继承和class继承

摘自http://www.liaoxuefeng.com/javascript

 

在传统的基于Class的语言如Java、C++中,继承的本质是扩展一个已有的Class,并生成新的Subclass。php

因为这类语言严格区分类和实例,继承其实是类型的扩展。可是,JavaScript因为采用原型继承,咱们没法直接扩展一个Class,由于根本不存在Class这种类型。java

可是办法仍是有的。咱们先回顾Student构造函数:浏览器

function Student(props) { this.name = props.name || 'Unnamed'; } Student.prototype.hello = function () { alert('Hello, ' + this.name + '!'); } 

以及Student的原型链:babel

js-proto

如今,咱们要基于Student扩展出PrimaryStudent,能够先定义出PrimaryStudent函数

function PrimaryStudent(props) { // 调用Student构造函数,绑定this变量: Student.call(this, props); this.grade = props.grade || 1; } 

可是,调用了Student构造函数不等于继承了StudentPrimaryStudent建立的对象的原型是:工具

new PrimaryStudent() ----> PrimaryStudent.prototype ----> Object.prototype ----> null 

必须想办法把原型链修改成:this

new PrimaryStudent() ----> PrimaryStudent.prototype ----> Student.prototype ----> Object.prototype ----> null 

这样,原型链对了,继承关系就对了。新的基于PrimaryStudent建立的对象不但能调用PrimaryStudent.prototype定义的方法,也能够调用Student.prototype定义的方法。spa

若是你想用最简单粗暴的方法这么干:prototype

PrimaryStudent.prototype = Student.prototype;

是不行的!若是这样的话,PrimaryStudentStudent共享一个原型对象,那还要定义PrimaryStudent干啥?

咱们必须借助一个中间对象来实现正确的原型链,这个中间对象的原型要指向Student.prototype。为了实现这一点,参考道爷(就是发明JSON的那个道格拉斯)的代码,中间对象能够用一个空函数F来实现:

// PrimaryStudent构造函数: function PrimaryStudent(props) { Student.call(this, props); this.grade = props.grade || 1; } // 空函数F: function F() { } // 把F的原型指向Student.prototype: F.prototype = Student.prototype; // 把PrimaryStudent的原型指向一个新的F对象,F对象的原型正好指向Student.prototype: PrimaryStudent.prototype = new F(); // 把PrimaryStudent原型的构造函数修复为PrimaryStudent: PrimaryStudent.prototype.constructor = PrimaryStudent; // 继续在PrimaryStudent原型(就是new F()对象)上定义方法: PrimaryStudent.prototype.getGrade = function () { return this.grade; }; // 建立xiaoming: var xiaoming = new PrimaryStudent({ name: '小明', grade: 2 }); xiaoming.name; // '小明' xiaoming.grade; // 2 // 验证原型: xiaoming.__proto__ === PrimaryStudent.prototype; // true xiaoming.__proto__.__proto__ === Student.prototype; // true // 验证继承关系: xiaoming instanceof PrimaryStudent; // true xiaoming instanceof Student; // true 

用一张图来表示新的原型链:

js-proto-extend

注意,函数F仅用于桥接,咱们仅建立了一个new F()实例,并且,没有改变原有的Student定义的原型链。

若是把继承这个动做用一个inherits()函数封装起来,还能够隐藏F的定义,并简化代码:

function inherits(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; } 

这个inherits()函数能够复用:



function Student(props) { this.name = props.name || 'Unnamed'; } Student.prototype.hello = function () { alert('Hello, ' + this.name + '!'); } function PrimaryStudent(props) { Student.call(this, props); this.grade = props.grade || 1; } // 实现原型继承链: inherits(PrimaryStudent, Student); // 绑定其余方法到PrimaryStudent原型: PrimaryStudent.prototype.getGrade = function () { return this.grade; };

小结

JavaScript的原型继承实现方式就是:

  1. 定义新的构造函数,并在内部用call()调用但愿“继承”的构造函数,并绑定this

  2. 借助中间函数F实现原型链继承,最好经过封装的inherits函数完成;

  3. 继续在新的构造函数的原型上定义新方法。



已属于懵逼状态。。。

class继承

在上面咱们看到了JavaScript的对象模型是基于原型实现的,特色是简单,缺点是理解起来比传统的类-实例模型要困难,最大的缺点是继承的实现须要编写大量代码,而且须要正确实现原型链。

有没有更简单的写法?有!

新的关键字class从ES6开始正式被引入到JavaScript中。class的目的就是让定义类更简单。

咱们先回顾用函数实现Student的方法:

function Student(name) { this.name = name; } Student.prototype.hello = function () { alert('Hello, ' + this.name + '!'); } 

若是用新的class关键字来编写Student,能够这样写:

class Student { constructor(name) { this.name = name; } hello() { alert('Hello, ' + this.name + '!'); } } 

比较一下就能够发现,class的定义包含了构造函数constructor和定义在原型对象上的函数hello()(注意没有function关键字),这样就避免了Student.prototype.hello = function () {...}这样分散的代码。

最后,建立一个Student对象代码和前面章节彻底同样:

var xiaoming = new Student('小明'); xiaoming.hello();

class定义对象的另外一个巨大的好处是继承更方便了。想想咱们从Student派生一个PrimaryStudent须要编写的代码量。如今,原型继承的中间对象,原型对象的构造函数等等都不须要考虑了,直接经过extends来实现:

class PrimaryStudent extends Student { constructor(name, grade) { super(name); // 记得用super调用父类的构造方法! this.grade = grade; } myGrade() { alert('I am at grade ' + this.grade); } } 

注意PrimaryStudent的定义也是class关键字实现的,而extends则表示原型链对象来自Student。子类的构造函数可能会与父类不太相同,例如,PrimaryStudent须要namegrade两个参数,而且须要经过super(name)来调用父类的构造函数,不然父类的name属性没法正常初始化。

PrimaryStudent已经自动得到了父类Studenthello方法,咱们又在子类中定义了新的myGrade方法。

ES6引入的class和原有的JavaScript原型继承有什么区别呢?实际上它们没有任何区别,class的做用就是让JavaScript引擎去实现原来须要咱们本身编写的原型链代码。简而言之,用class的好处就是极大地简化了原型链代码。

你必定会问,class这么好用,能不能如今就用上?

如今用还早了点,由于不是全部的主流浏览器都支持ES6的class。若是必定要如今就用上,就须要一个工具把class代码转换为传统的prototype代码,能够试试Babel这个工具。

相关文章
相关标签/搜索