// 定义一个动物类 function Animal (name) { // 属性 this.name = name || ‘Animal’; // 实例方法 this.sleep = function(){ console.log(this.name + ‘正在睡觉!’); } } // 原型方法 Animal.prototype.eat = function(food) { console.log(this.name + ‘正在吃:’ + food); };
核心:将父类的实例做为子类的原型, JavaScript常见的六种继承方式
function Cat(){ } Cat.prototype = new Animal(); Cat.prototype.name = ‘cat’; // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.eat(‘fish’)); console.log(cat.sleep()); console.log(cat instanceof Animal); //true console.log(cat instanceof Cat); //true
特色:html
缺点:es6
下面代码解释缺点3(注意是引用属性):segmentfault
function Super(){ this.val = 1; this.arr = [1]; } function Sub(){ // ... } Sub.prototype = new Super(); // 核心 var sub1 = new Sub(); var sub2 = new Sub(); sub1.val = 2; sub1.arr.push(2); alert(sub1.val); // 2 alert(sub2.val); // 1 alert(sub1.arr); // 1, 2 alert(sub2.arr); // 1, 2
核心:使用父类的构建函数来加强子类实例,等于复制父类的实例属性给子类(没用到原型),除了call方法,也能够用apply()
function Cat(name){ Animal.call(this); this.name = name || ‘Tom’; } // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // false console.log(cat instanceof Cat); // true
特色:app
缺点:函数
核心:为父类实例添加新特性,做为子类实例返回
function Cat(name){ var instance = new Animal(); instance.name = name || ‘Tom’; return instance; } // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); // false
特色:性能
缺点:this
核心:使用for...in将父类实例中的方法赋给子类实例
function Cat(name){ var animal = new Animal(); for(var p in animal){ Cat.prototype[p] = animal[p]; } Cat.prototype.name = name || ‘Tom’; } // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // false console.log(cat instanceof Cat); // true
特色:prototype
缺点:code
核心:经过调用父类构造,继承父类的属性并保留传参的优势,而后经过将父类实例做为子类原型,实现函数复用
function Cat(name){ Animal.call(this); this.name = name || ‘Tom’; } Cat.prototype = new Animal(); //组合继承须要修复构造函数的指向 Cat.prototype.constructor=Cat; // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); // true
特色:htm
缺点:
核心:经过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点
function Cat(name){ Animal.call(this); this.name = name || ‘Tom’; } (function(){ // 建立一个没有实例方法的类 var Super = function(){}; Super.prototype = Animal.prototype; //将实例做为子类的原型 Cat.prototype = new Super(); //寄生组合继承须要修复构造函数的指向 Cat.prototype.constructor=Cat; })(); // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); //true
特色:
缺点:
补充:es6的class实现继承
class Animal { //构造函数 constructor(props) { this.name = props.name || '未知'; } eat() { alert(this.name + "在吃东西..."); } } //class继承 class Bird extends Animal { //构造函数 constructor(props) { //调用实现父类的构造函数 super(props); this.name = props.name || "未知"; } fly() { alert(this.name + "在飞..."); } } var myBird = new Bird({ name: '鹦鹉' }) myBird.eat() // 鹦鹉在吃东西... myBird.fly() // 鹦鹉在飞...