一、原型继承
要实现继承,首先需要一个父类。
定义一个动物类:
核心: 将父类的实例作为子类的原型
function Animal(name){
this.name=name ||"animal"; //属性
this.eat=function(){
console.log(this.name+"在次饭饭!");
}
}
Animal.prototype.sex="公";
Animal.prototype.weight="20kg"; // 属性默认值
Animal.prototype.sleep=function(){
/*在控制台输出这行后,调用方法时控制台会返回undefined*/
// console.log(this.name+"正在睡觉~");
return this.name+"正在睡觉"; //改用 return 给一个返回值就好了
};
//声明一个子类
function Dog(){
}
原型链继承,直接原型一个父类:
Dog.prototype=new Animal(); //(1) 此处可以向父类传参
Dog.prototype.name="小狗";
/* 为子类新增的属性和方法必须在实例化对象之后,即在(1)之后。*/
Dog.prototype.type="犬类";
Dog.prototype.weight = "30kg"; // 设置属性值
Dog.prototype.sleepr=function(){
return this.name+"睡觉!"
};
var dog=new Dog(); // 此处传参无用
console.log(dog.sleepr());//小狗睡觉!
console.log(dog.name); //小狗
console.log(dog.type); //犬类
console.log(dog.sleep()); //小狗正在睡觉
console.log(dog.weight); //30kg
console.log(dog instanceof Dog); //true
console.log(dog instanceof Animal); //true
动物实例化为对象:
var animal = new Animal("猫");
console.log(animal.name); //猫
console.log(animal.sleep()); //猫正在睡觉
animal.eat(); //猫正在次饭饭!
// console.log(animal.eat()); //会同时输出一个undefined
console.log(animal.sex); // 公
特点:
非常纯粹的继承关系,实例是子类的实例,也是父类的实例;父类新增原型方法/原型属性,子类都能访问到;简单,易于实现。
缺点:
要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行,不能放到构造器中无法实现多继承。
创建子类实例时,无法向父类构造函数传参。
二、构造继承
核心:使用父类的构造函数来增强子类实例,等于是复制父类的实例属性给子类(没用到原型)
function Animal(name,sex,age){
this.name=name;
this.sex=sex;
this.age=age;
this.drink= function () {
return this.name+"正在喝水。。"
}
}
Animal.prototype.sleep=function(){
return this.name+"正在午休";
};
function Bird(name,sex,age){
Animal.apply(this);
Type.apply(this);
this.name=name;
this.sex=sex;
this.age=age;
}
var bird=new Bird("鸟","公",3);
console.log(bird.name); //鸟
console.log(bird.drink());//鸟正在喝水。。
// console.log(bird.sleep()); // 会报错! 组合继承可以解决。
console.log(bird.fly());//can fly!
console.log(bird.sex);//公
console.log(bird.age);//3
console.log(bird instanceof Bird);//true
console.log(bird instanceof Animal);//false
特点:
创建子类实例时,可以向父类传递参数;可以实现多继承(call多个父类对象)。
缺点:
实例并不是父类的实例,只是子类的实例;只能继承父类的实例属性和方法,不能继承原型属性/方法;无法实现函数复用,每个子类都有父类实例函数的副本,影响性能。
三、实例继承
function Animal(name){
this.run=function(){
return this.name + "在跑~";
}
}
Animal.prototype.walk=function(){
return this.name + "在走~";
};
function Koala(){
this.hao="nice";
var instances=new Animal();
instances.name="考拉";
return instances; // 返回这个父类
}
Koala.prototype.sleep=function(){
return this.name+"睡觉"
};
var koala=new Koala(); // 实例是父类的实例,不是子类的实例
console.log(koala.name);//考拉
console.log(koala.run());//考拉在跑~
console.log(koala.walk());//考拉在走~
// console.log(koala.hao); //undefined
// console.log(koala.sleep()); //报错,不能继承子类的属性 ??那怎么能继承呢?
特点:
不限制调用方式,不管是new 子类()还是子类(),返回的对象具有相同的效果。
缺点:
实例是父类的实例,不是子类的实例;不支持多继承。
四、组合继承
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';
}
Cat.prototype=new Animal();
var cat = new Cat();
console.log(cat.name); //Tom
console.log(cat.sleep()); //Tom正在睡觉!
console.log(cat.eat("cookie")); //Tom正在吃:cookie
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true
特点:
弥补了构造继承的缺陷。
缺点:
生成了两个实例,消耗内存;父级的构造函数执行了两次。
组合继承的优化1
组合继承的优化2*