Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 522 Bytes

继承.md

File metadata and controls

28 lines (25 loc) · 522 Bytes
  1. 原型链继承
//定义一个动物类
function Animal (name){
  this.name = name || 'animal';
  this.sleep = function (){
    console.log(this.name + '正在睡觉')
  }
}
//原型方法
Animal.prototype.eat = function(food){
  console.log(this.name + '正在吃东西'+ food)
}
//通过原型链继承
function Cat (){
  
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';
cat =new Cat()
console.log(cat.eat('sth'))
console.log(cat.sleep())

  1. 借用构造函数继承