类的创建和继承,原型链

类的创建new一个function,在这个function的prototype里面增加属性和方法。

原型继承: 无法给构造函数传递参数,改变不了里面的值
Student.prototype = new Person();
Student.prototype.constructor = Student;

借用构造函数可以继承属性。  call()  改变函数中的this,直接调用函数     Person.call(this, name, age, sex);    this指向Person
如果创建多个对象,里面的方法就会继承很多个,造成重复,故方法不能继承

组合继承:   构造继承 + 原型继承 
Person.call(this, name, age, sex);
通过原型,让子类型,继承父类型中的方法
Student.prototype = new Person();   
Student.prototype.constructor = Student;

寄生组合继承
其他的和组合继承一样      Object.create()使用指定的原型对象及其属性去创建一个新的对象
Student.prototype= Object.create(Person.prototype);

猜你喜欢

转载自blog.csdn.net/qq_41160739/article/details/113032261