JS实现继承的常用方法
1、构造函数继承 function Parent(name){ this.name=name; } Parent.prototype.saiHi=function(){ console.log("hello") } function child(name,age,gender){ Parent.call(this,name) //call改变了this指向,这里指向了 child this.age=age this.gender=gender } let child=new Child("王磊",20,"男") console.log(child.name);// 王磊 child.sayHi(); // Uncaught TypeError:child.sayHi is not a function 缺点:只能解决属性的继承,使用属性的值不重复,但是父级类别的方法不能继承 2、原型链继承 function Parent(name,gender){ this.name=name; this.gender=gender; this.list=[1,2,3] } Parent.prototype.eat=function(){ console.log("晚餐时间到") } function Child(age){ this.age=age; } Child.prototype=new