构造函数绑定的方式实现继承,通过call和apply的方式,在子类的内部调用父类的构造函数方法 function Person(){ this.foot=2 } function Student(){ Person.call(this)}这样Student内就有foot了,实现了继承 拷贝继承 把父对象的所有属性和方法,拷贝进子对象 将父对象的prototype对象中的属性,一一拷贝给child对象的prototype对象 function Person(){} Person.prototype.head=1; Person.prototype.foot=2; Person.prototype.eat=function(){ document.write('我会吃饭') }
function Student(){}
function extend(child,parent){
for(let key in parent.prototype){
child.prototype[key]=parent.prototype[key]
}
}
extend(Student,Person)
let stu1=new Student()
stu1.eat();
组合继承也叫伪经典继承
将原型链继承和构造函数继承组合在一块
原型链实现对原型属性和方法的继承
借用构造函数实现对实例属性的继承
function Person(){
实例属性
this.head=1;
this.foot=2;
}
原型属性和方法
Person.prototype.weight='70kg';
Person.prototype.eat=function(){
console.log('我会吃饭');
}
function Student(){
借用构造函数实现对实例属性的继承
Person.call(this)
}
原型链实现对原型属性和方法的继承
Student.prototype=new Person();
Student.prototype.contructor=Student;
let stu1=new Student();
stu1.eat();
call和apply是用来改变this指向的