javascript继承
讲解javascript各种继承方式的优缺点,信息来自《javascript高级程序设计》
原型链继承
function Parent(){
this.name = 'kevin';
}
Parent.prototype.getName = function(){
console.log(this.name);
}
function Child(){
}
Child.prototype = new Parent();
const child1 = new Child();
console.log(child1.getName())
问题:
1、 应用类型的属性被所有实例共享,举例:
function Parent(){
this.names = ['Kevin','Daisy']
}
function Child(){
}
Child.prototype = new Parent();
const child1 = new Child();
child1.names.push('XiaoBo');
console.log(child1.names)//["Kevin", "Daisy", "XiaoBo"]
const child2 = new Child();
console.log(child2.names)//["Kevin", "Daisy", "XiaoBo"]
- 在创建Child的实例时不能向Parent传参
借用构造函数(经典继承)
function Parent(){
this.names = ["Kevin", "Daisy"]
}
function Child(){
Parent.call(this);
}
const child1 = new Child();
child1.names.push('XiaoBo');
console.log(child1.names)//["Kevin", "Daisy", "XiaoBo"]
const child2 = new Child();
console.log(child2.names)//["Kevin", "Daisy"]
优点:
- 避免了引用类型的属性被所有实例共享
- 可以在Child中向Parent传值
举例:
function Parent(name){
this.name = name;
}
function Child(name){
Parent.call(this,name);
}
const child1 = new Child('Kevin');
console.log(child1.name); // Kevin
const child2 = new Child('Daisy');
console.log(child2.name); // Daisy
缺点:
方法都在构造函数中定义,每次创建实例都会创建一遍方法。
组合继承
原型链继承和经典继承双剑合璧
function Parent(name){
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function(){
return this.name;
}
function Child(name,age){
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
var child1 = new Child('Kevin', 18)
console.log(child1);
优点:融合原型链继承和构造函数继承的优点,是javascript中最常见的继承模式。
原型式继承
functio createObj(obj){
function F(){}
F.prototype = 0;
return new F();
}
就是ES5 Object.create的模拟实现,将传入的对象作为创建的对象的原型。
缺点:
包含引用类型的值使用都会共享相应的值,这点跟原型链继承一样
const person = {
name: 'Kevin',
friends: ['daisy', 'kelly']
}
const person1 = createObj(person);
const person2 = createObj(person);
寄生式继承
创建一个仅用于封装集成过程的函数,该函数在内部以某种形式做增强对象,最后返回对象。
function createObj(o){
const clone = Object.create(o);
o.sayName = function(){
console.log('hi')
}
return clone;
}
缺点:和借用构造函数一样,每次创建对象都会创建一遍对象
寄生组合式继承
组合继承最大的缺点就是会调用两次父构造函数,造成属性重复。
一次是设置子类实例的原型的时候
Child.prototype = new Parent();
一次在创建子类实例的时候
const child1 = new Child();
//Child构造函数内部有如下代码
Parent.call(this, name);
在这里又一次调用了Parent。
所以在这个例子里,打印child1会发现Child.rototype和child1都有一个属性colors,属性值为['red', 'blue', 'green']。
所以需要解决如何避免这一次重复调用,如果不使用Child.prototype = new Parent();,而是简介让Child.prototype访问到Parent.prototype?
function Parent(name){
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function(){
console.log(this.name)
}
function Child(name, age){
Parent.call(this, name);
this.age = age;
}
const F = function(){}
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
const child1 = new Child('Keniv', 14);
console.log(child1)
封装一下寄生组合式继承
function object(0){
function F(){};
F.prototype = o;
return new F();
}
function prototype(child, parent){
const prototype = object(parent.prototype);
prototype.constructor = child;
child.prototype = prototype;
}
prototype(Child, Parent)
这种方式的高效率体现在它只调用了一次Parent构造函数,并且因此避免了在Parent.prototype上面创建不必要的、多余的属性。与此同时,原型链还能保持不变,因此能够正常使用instancesof和isPrototypeOf。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。