原型链的继承

我们需要把原型链的继承关系改为这样的:

student => Student.prototype => Person.prototype => Object.prototype => null

因此,我们需要把Student的原型指向父类Person的一个实例对象:

//创建一个父类构造函数的实例

person var person = new Person();

//把Student的原型指向新创建的 person实例

Student.prototype = person;

//同时需要把Student的构造函数修正为Student,

//因为此时Student的构造函数为Person,可自行 console.log(Student.prototype.constructor)查看

Student.prototype.constructor = Student;

//此时就可以正常的调用父类的方法了

student.getName(); //姓名:小明

posted @ 2020-03-25 16:51  清明|雨上  阅读(52)  评论(0)    收藏  举报