1.原型链继承
核心: 将父类的实例作为子类的原型
缺点:
1.要想为子类新增原型属性和方法,必须要在new Parent()这样的语句之后执行
2.无法实现多继承。
3.来自父类原型对象的所有属性被所有实例共享
4.创建子类实例时,无法向父类构造函数传参。
function Parent(age) {
this.age = age;
};
Parent.prototype.friends = ['a', 'b'];
Parent.prototype.getAge = function () { //对原型进行扩展
return this.age;
};
function Child(sex) {
this.sex = sex;
};
Child.prototype = new Parent(29); //这一句是关键 //通过构造器函数创建出一个新对象,把老对象的东西都拿过来。
Child.prototype.getSex = function () {
return this.sex;
};
// Child.prototype.getAge = function () { //可以重写从父类继承来的方法,会优先调用自己的。
// console.log(222);
// };
var result = new Child('male');
var temp = new Child('female');
temp.friends.push('c');
console.log(result.friends); // ['a', 'b', 'c']
console.log(result.getSex()); // output: male //调用了从 Parent 原型中继承来的方法(继承到了当前对象的原型中)
console.log(result.getAge()); // output: 29 //调用了从 Child 原型中扩展来的方法
2.构造继承
基本思想: 借用构造函数的基本思想就是利用call或者apply把父类中通过this指定的属性和方法复制(借用)到子类创建的实例中.
因为this对象是在运行时基于函数的执行环境绑定的.也就是说,在全局中, this等于window,而当函数被作为某个对象的方法调用时, this等于那个对象.
call、apply方法可讲一个函数的底箱上下文从初始的上下文改变为由 thisObj指定的新对象.
所以,这个借用构造函数就是, new对象的时候(new创建的时候, this指向创建的这个实例), 创建了一个新的实例对象,
并且执行Child里面的代码, 而Child里面用call调用了Parent,也就是说 把this指向改成了指向新的实例,
所以就会把Child里面的this县官属性和方法复制到新的实例上, 而不是赋值到Parent上面,所以所有实例中就拥有了父类定义的这些this的属性和方法.
因为属性是绑定到this上面的, 所以调用的时候才赋到相应的实例中, 各个实例的值就不会互相影响了.
核心: 使用父类构造函数来增强子类实例, 等于是赋值父类的实例属性给子类(没用到原型);
缺点: 方法都在构造函数中定义, 只能继承父类的实例属性和方法, 不能继承原型属性/方法, 无法实现函数复用, 每个子类都有父类实例函数的副本, 影响性能
function Parent(age){
this.age = age;
this.friends = ['a', 'b'];
this.getAge = function() {
return this.age;
}
}
Parent.prototype.consoleLog = function () { // 对原型进行扩展的方法就无法复用了
console.log('这是原型方法');
}
function Child(sex) {
Parent.call(this, '29');
this.sex = sex;
}
var result = new Child('male');
console.log(result.age); // 29
console.log(result.friends); // [a, b]
console.log(result.getAge()); // 29
console.log(result.sex); // male
console.log(result.consoleLog()); // ❌ TypeError: result.consoleLog is not a function
3.组合继承
组合继承(所有的实例都能拥有自己的属性,并且可以使用相同的方法,组合继承避免了原型链和借用构造函数的缺陷,结合了两个的优点,是最常用的继承方式)
核心:通过调用父类构造,继承父类的属性并保留传参的优点,然后再通过将父类实例作为子类原型,实现函数复用
缺点:调用了两次父类构造函数,生成了两份实例(子类实例将子类原型上的那份屏蔽了)
function Parent(age) {
this.age = age;
this.friends = ['a', 'b'];
};
Person.prototype.getAge = function () {
return this.age;
};
function Child(sex) {
Parent.call(this, '29'); //这一步很关键
this.sex = sex;
};
Child.prototype = new Parent('29'); //这一步也很关键
var result = new Child('male');
console.log(result.age); // 29
result.friends.push("c");
console.log(result.friends); // ['a','b','c']
console.log(result.getAge()); // 29
console.log(result.sex); // male
var result1 = new Child('female'); //通过借用构造函数都有自己的属性,通过原型享用公共的方法
console.log(result1.sex); //female
console.log(result1.friends); //['a','b']
4.寄生组合继承
核心:通过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点
缺点:堪称完美,但实现较为复杂
function Parent(age) {
this.age = age;
this.friends = ['a','b'];
}
Parent.prototype.getAge = function () {
return this.age;
};
function Child(sex) {
Parent.call(this, '29');
this.sex = sex;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
var result = new Child('male');
console.log(result.age);
console.log(result.friends);
console.log(result.getAge());
console.log(result.sex);
console.log(result.constructor)