原型链继承

function Person(){
    
}
function Student(){
    
}

让Student继承Person,有以下三种方式:

Student.prototype=Person.prototype;
Student.prototype=new Person();
Student.prototype=Object.create(Person.prototype);
Student.prototype.contructor=Person;

方法一:
Student.prototype=Person.prototype;
改变student的同时,改变了person,比如学生会学习,person并不一定,并不是我们想要的。


方法二:
Student.prototype=new Person();
传参数的时候,并不是最理想的

方法三:
Student.prototype=Object.create(Person.prototype);

Object.create()是ES5的语法,需要兼容,代码如下:

if(!Object.create){
Object.create=function(proto){
function F(){}
F.prototype=proto;
return new F;
}
}


原文地址:https://www.cnblogs.com/xiaotaiyang/p/5797160.html