js实现继承的几种方式

// 定义一个动物类
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(name){
  Animal.call(this);
  this.name = name || 'Tom';
}
(function(){
  // 创建一个没有实例方法的类
  var Super = function(){};
  Super.prototype = Animal.prototype;
  //将实例作为子类的原型
  Cat.prototype = new Super();
})();
Cat.prototype.run = function(){
    console.log(this.name+"正在抓老鼠");
}

// Test Code
var cat = new Cat("jeetty");
console.log(cat.name);
cat.sleep();
cat.eat('饭');
cat.run();
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true
原文地址:https://www.cnblogs.com/as3lib/p/9740145.html