ES6学习05--继承和ES5继承的区别

ES6继承和ES5继承

    • 1.ES5 继承回顾
    • 2. ES6 类
    • 3. ES6 继承

1.ES5 继承回顾

在ES5 中,我们使用组合继承,通过原型链继承继承原型上的公共属性和公共方法,而通过经典继承函数继承实例属性。继承中,如果不把子类的构造函数再指回自身构造函数,就会很混乱,子类的类型居然是父类类型。如下案例,如果不使用该语句:Dog.prototype.constructor= Dog,那么Dog的实例居然是Animal。

function Animal(name,age,weight){
   
  this.name=name
  this.age=age
  this.weight=weight
}
Animal.prototype={
   
  constructor:Animal,
  sayName(){
   
    console.log(this.name);
  }
}
function Dog(name,age,weight,type){
   
  // 借用构造函数继承属性
  Animal.call(this,name,age,weight)
  this.type=type
}
// 继承父类的方法
Dog.prototype

你可能感兴趣的:(ES6,javascript,es6,原型模式,前端)