// 定义一个动物类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 Codevar cat = new Cat("jeetty");console.log(cat.name);cat.sleep();cat.eat(‘饭‘);cat.run();console.log(cat instanceof Animal); // trueconsole.log(cat instanceof Cat); //true
js实现继承的几种方式
原文地址:https://www.cnblogs.com/as3lib/p/9740145.html