通俗的理解,子女会继承父母的一些体态样貌特征,拥有一些与原型相同的属性就是继承,JS中的继承就是让子类对象拥有其父类对象的属性和方法。
1.原型链继承
让一个构造函数的原型是另一个类型的实例,那么这个构造函数new出来的实例就具有该实例的属性。
当试图访问一个对象的属性时,它不仅仅在该对象上搜寻,还会搜寻该对象的原型,以及该对象的原型的原型,依次层层向上搜索,直到找到一个名字匹配的属性或到达原型链的末尾。
function Parent1() {
this.name = 'parent1';
this.play = [1, 2, 3]
}
function Child1() {
this.type = 'child2';
}
Child1.prototype = new Parent1();
console.log(new Child1());
优点:
1.非常纯粹的继承关系,实例是子类的实例,也是父类的实例
2.父类新增原型方法/原型属性,子类都能访问到
3.简单,易于实现
缺点:
1.要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行,不能放到构造器中
2.无法实现多继承
3.来自原型对象的所有属性被所有实例共享(来自原型对象的引用属性是所有实例共享的)
4.创建子类实例时,无法向父类构造函数传参
2.构造函数继承
function Parent1(){
this.name = 'parent1';
}
Parent1.prototype.getName = function () {
return this.name;
}
function Child1(){
Parent1.call(this);
this.type = 'child1'
}
let child = new Child1();
console.log(child); // 没问题
console.log(child.getName()); // 会报错
优点:子类构建时可以向父类传参
缺点:无法复用父类原型属性和方法
3.原型链与构造函数同时使用,即组合继承
function animal(num){
this.eyesNum = num;
this.leg= {leftLeg:"短",rightLeg:"长"}
};
animal.prototype.sex = {x:"男",y:"女"};
function fish(color,...args){
animal.apply(this,args);
this.color = color;
}
fish.prototype = new animal(2);
fish.prototype.constructor = fish;
var fish1 = new fish("黑",4);
var fish2 = new fish("白",3);
fish1.leg.leftLeg = "长";
此时打印,各实例私有属性相互独立,原型属性可查,结合了两者优点,唯一不足在创建实例过程中调用了两次构造函数,由于赋值原型的同时使用了apply方法,子类和父类中各有一套私有属性,增加了内存开销。
4.原型式继承
let parent4 = {
name: "parent4",
friends: ["p1", "p2", "p3"],
getName: function() {
return this.name;
}
};
let person4 = Object.create(parent4);
person4.name = "tom";
person4.friends.push("jerry");
let person5 = Object.create(parent4);
person5.friends.push("lucy");
通过 Object.create 这个方法可以实现普通对象的继承,不仅仅能继承属性,同样也可以继承 getName 的方法。
5.寄生式继承
let parent5 = {
name: "parent5",
friends: ["p1", "p2", "p3"],
getName: function() {
return this.name;
}
};
function clone(original) {
let clone = Object.create(original);
clone.getFriends = function() {
return this.friends
};
return clone;
}
寄生式继承的思路与(寄生) `原型式继承` 和 `工厂模式` 似, 即创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真的是它做了所有工作一样返回对象。
6.寄生组合式继承
function clone (parent, child) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
}
function Parent6() {
this.name = 'parent6';
this.play = [1, 2, 3];
}
Parent6.prototype.getName = function () {
return this.name;
}
function Child6() {
Parent6.call(this);
this.friends = 'child5';
}
clone(Parent6, Child6);
Child6.prototype.getFriends = function () {
return this.friends;
}
优点:集各模式所长, 总体来说就是解决了 组合模式 两次调用父类构造方法的弊端,减少子类父类属性重复的问题,减少了内存占用。
缺点:实现代码相对来说复杂,冗长,需要封装一下才好