JavaScript深刻系列第十五篇,讲解JavaScript各类继承方式和优缺点。git
本文讲解JavaScript各类继承方式和优缺点。github
可是注意:闭包
这篇文章更像是笔记,哎,再让我感叹一句:《JavaScript高级程序设计》写得真是太好了!app
function Parent () {
this.name = 'kevin';
}
Parent.prototype.getName = function () {
console.log(this.name);
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
console.log(child1.getName()) // kevin复制代码
问题:函数
1.引用类型的属性被全部实例共享,举个例子:ui
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy", "yayu"]复制代码
2.在建立 Child 的实例时,不能向Parent传参this
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
Parent.call(this);
}
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy"]复制代码
优势:spa
1.避免了引用类型的属性被全部实例共享prototype
2.能够在 Child 中向 Parent 传参设计
举个例子:
function Parent (name) {
this.name = name;
}
function Child (name) {
Parent.call(this, name);
}
var child1 = new Child('kevin');
console.log(child1.name); // kevin
var child2 = new Child('daisy');
console.log(child2.name); // daisy复制代码
缺点:
方法都在构造函数中定义,每次建立实例都会建立一遍方法。
原型链继承和经典继承双剑合璧。
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;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
child1.colors.push('black');
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]
var child2 = new Child('daisy', '20');
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]复制代码
优势:融合原型链继承和构造函数的优势,是 JavaScript 中最经常使用的继承模式。
function createObj(o) {
function F(){}
F.prototype = o;
return new F();
}复制代码
就是 ES5 Object.create 的模拟实现,将传入的对象做为建立的对象的原型。
缺点:
包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承同样。
var person = {
name: 'kevin',
friends: ['daisy', 'kelly']
}
var person1 = createObj(person);
var person2 = createObj(person);
person1.name = 'person1';
console.log(person2.name); // kevin
person1.firends.push('taylor');
console.log(person2.friends); // ["daisy", "kelly", "taylor"]复制代码
注意:修改person1.name
的值,person2.name
的值并未发生改变,并非由于person1
和person2
有独立的 name 值,而是由于person1.name = 'person1'
,给person1
添加了 name 值,并不是修改了原型上的 name 值。
建立一个仅用于封装继承过程的函数,该函数在内部以某种形式来作加强对象,最后返回对象。
function createObj (o) {
var clone = object.create(o);
clone.sayName = function () {
console.log('hi');
}
return clone;
}复制代码
缺点:跟借用构造函数模式同样,每次建立对象都会建立一遍方法。
为了方便你们阅读,在这里重复一下组合继承的代码:
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;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
console.log(child1)复制代码
组合继承最大的缺点是会调用两次父构造函数。
一次是设置子类型实例的原型的时候:
Child.prototype = new Parent();复制代码
一次在建立子类型实例的时候:
var child1 = new Child('kevin', '18');复制代码
回想下 new 的模拟实现,其实在这句中,咱们会执行:
Parent.call(this, name);复制代码
在这里,咱们又会调用了一次 Parent 构造函数。
因此,在这个例子中,若是咱们打印 child1 对象,咱们会发现 Child.prototype 和 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;
}
// 关键的三步
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
var child1 = new Child('kevin', '18');
console.log(child1);复制代码
最后咱们封装一下这个继承方法:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
function prototype(child, parent) {
var prototype = object(parent.prototype);
prototype.constructor = child;
child.prototype = prototype;
}
// 当咱们使用的时候:
prototype(Child, Parent);复制代码
引用《JavaScript高级程序设计》中对寄生组合式继承的夸赞就是:
这种方式的高效率体现它只调用了一次 Parent 构造函数,而且所以避免了在 Parent.prototype 上面建立没必要要的、多余的属性。与此同时,原型链还能保持不变;所以,还可以正常使用 instanceof 和 isPrototypeOf。开发人员广泛认为寄生组合式继承是引用类型最理想的继承范式。
《JavaScript深刻之call和apply的模拟实现》
JavaScript深刻系列目录地址:github.com/mqyqingfeng…。
JavaScript深刻系列预计写十五篇左右,旨在帮你们捋顺JavaScript底层知识,重点讲解如原型、做用域、执行上下文、变量对象、this、闭包、按值传递、call、apply、bind、new、继承等难点概念。
若是有错误或者不严谨的地方,请务必给予指正,十分感谢。若是喜欢或者有所启发,欢迎star,对做者也是一种鼓励。