JS原型链下的继承实现

该文为廖雪峰原型继承教程的学习笔记,教程地址为:原型继承
一:首先,介绍下如何从一个对象创建出另一个对象,这个是谈论继承的基础。 【JS原型链下的继承实现】1:Object.create()方法
该方法可以从一个普通对象中,创建出一个新的对象,新创建对象的__proto__指向普通对象

var a = {name: 'Tom', age: 14} var b = Object.create(a) console.log(a) console.log(typeof a) console.log(b) console.log(typeof b) console.log(b.__proto__ === a)

2:从函数对象中创建
该方法从函数对象中创建一个实例,实例是一个普通对象,实例的__proto__ 指向函数对象的prototype
var Student = function (name, age) { this.name = name ? name : 'Tom' this.age = age ? age : 14 } var jessy = new Student('Jessy', 17)) console.log(jessy) console.log(typeof jessy) console.log(jessy.__proto__ === Student.prototype)

二:原型继承 1:首先我们创建一个原始函数对象
function Student(name, age) { this.name = name ? name : 'Unnamed' this.age = age ? age : '0' }Student.prototype.hello = function () { console.log('Hello, ' + this.name + '!'); } console.log(Student) console.log(Student.prototype)

上面的代码创建一个函数对象Student,它拥有两个属性name和age。在它的原型对象(prototype)上,我们新增了一个hello方法,它的原型对象同时还有一个所有函数对象的prototype自带的constructor方法,constructor指向函数对象本身。
如果我们使用Student函数对象按照第一部分的第2种方法,则代码如下
var tom = new Student('Tom', 14) console.log(tom) console.log(typeof tom) console.log(tom.__proto__ === Student.prototype)

其中的tom为Student的一个实例,实例首先会增加Student的两个属性name和age。然后增加一个__proto__指向Student.prototype
2:然后我们创建一个PrimaryStudent,用来继承Student
function PrimaryStudent (name, age, grad) { Student.call(this, name, age) this.gard = grad ? grad : 0 } var jessy = new PrimaryStudent('jessy', 17, 8) console.log(jessy) console.log(typeof jessy) console.log(jessy.hello())

但是当上面的代码执行的时候,你会发现jessy.hello,并不存在,也就是说PrimaryStudent并没有继承到Student。这是因此,此时的原型链的结构如下
jessy.__proto__ === PrimaryStudent.prototype PrimaryStudent.prototpe.__proto__ === Object.prototype Object.prototype.__proto__ === null

中间并没有引入Student.prototype,而只是通过call函数,执行了Student函数对象的代码,使得PrimaryStudent的实例jessy拥有了name和age两个属性而已。
如果要成功继承Student,则相应的原型链应该如下:
jessy.__proto__ === PrimaryStudent.prototype PrimaryStudent.prototpe.__proto__ === Student.prototype Student.prototype.__proto__ === Object.prototype Object.prototype.__proto__ === null

和上面的作比较,我们可以发现,我们需要新建一个Object,该Object的__proto__指向Student.prototype。并且使得PrimaryStudent.prototype的__proto__指向新建的Object。
此时,我们再回过头来,看一下第一部分的内容,如果采用第1种方法,那么代码如下
PrimaryStudent.prototype = Object.create(Student.prototype) PrimaryStudent.prototype.constructor = PrimaryStudent// this line used to re-point to PrimaryStudent console.log(PrimaryStudent.prototype.__proto__ === Student.prototype) console.log(PrimaryStudent.prototype)

如果采用第2种方法,那么代码如下:
PrimaryStudent.prototype = new Student() PrimaryStudent.prototype.constructor = PrimaryStudent// this line used to re-point to PrimaryStudent console.log(PrimaryStudent.prototype.__proto__ === Student.prototype) console.log(PrimaryStudent.prototype)

如果你两种方法都实现了一遍,那么你会发现两种方法的输出并不相同,虽然两种方法中PrimaryStudent.prototype.__proto__都指向了Student.prototype。但是PrimaryStudent.prototype包含的属性却并不相同,第2种方法的实现相对于第1种,多出了Student实例所拥有的name和age属性,而在正常的原型链继承中这些都是不应该出现的。原型链继承,应该只继承原型对象中的属性,而不应该继承对象实例中才会出现的属性。
之所以出现这种情况,是因为使用Object.create的时候,只会将PrimaryStudent.prototype中的__proto__指向Student这个函数的原型对象Student.prototype,而使用new Student()的时候,PrimaryStudent.prototype指向的是Student这个函数对象的一个新建实例,Student函数对象在实例化的时候,会将新建对象的__proto__指向Student.prototype,也会增加Student中的属性,比如name,age。
因此,我们应该使用第1种方法实现原型链继承,其代码如下:
function Student(name, age) { this.name = name ? name : 'Unnamed' this.age = age ? age : '0' }Student.prototype.hello = function () { console.log('Hello, ' + this.name + '!') }function PrimaryStudent (name, age, grad) { Student.call(this, name, age) this.gard = grad ? grad : 0 }PrimaryStudent.prototype = Object.create(Student.prototype) PrimaryStudent.prototype.constructor = PrimaryStudentPrimaryStudent.prototype.getGrade = function () { return this.grade; } // 创建xiaoming: var xiaoming = new PrimaryStudent('小明', 17, 2) xiaoming.name // '小明' xiaoming.grade // 2 xiaoming.hello()// 验证原型: xiaoming.__proto__ === PrimaryStudent.prototype // true xiaoming.__proto__.__proto__ === Student.prototype // true// 验证继承关系: xiaoming instanceof PrimaryStudent // true xiaoming instanceof Student // true

另外,除了第1种方法,还有使用架桥函数的方法,其代码如下,具体的分析不再多述:
function Student(name, age) { this.name = name ? name : 'Unnamed' this.age = age ? age : '0' }Student.prototype.hello = function () { console.log('Hello, ' + this.name + '!') }function PrimaryStudent (name, age, grad) { Student.call(this, name, age) this.gard = grad ? grad : 0 }function F () { // empty function } // 把F的原型指向Student.prototype: F.prototype = Student.prototype; PrimaryStudent.prototype = new F() PrimaryStudent.prototype.constructor = PrimaryStudentPrimaryStudent.prototype.getGrade = function () { return this.gard ; } // 创建xiaoming: var xiaoming = new PrimaryStudent('小明', 17, 2) xiaoming.name // '小明' xiaoming.gard // 2 xiaoming.hello()// 验证原型: xiaoming.__proto__ === PrimaryStudent.prototype // true xiaoming.__proto__.__proto__ === Student.prototype // true// 验证继承关系: xiaoming instanceof PrimaryStudent // true xiaoming instanceof Student // true

    推荐阅读