JavaScript深入之继承的多种方式和优缺点

更新日期: 2020-03-29阅读: 1.6k标签: 继承

写在前面

本文讲解JavaScript各种继承方式和优缺点。

但是注意:这篇文章更像是笔记,哎,再让我感叹一句:《JavaScript高级程序设计》写得真是太好了!


1、原型链继承

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、引用类型的属性被所有实例共享,举个例子:

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传参


2、借用构造函数(经典继承)

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"] 

优点:

1、避免了引用类型的属性被所有实例共享

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 

缺点:

方法都在构造函数中定义,每次创建实例都会创建一遍方法。


3、组合继承

原型链继承和经典继承双剑合璧。

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();  
ChildChild.prototype.constructor = Child;  
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 中最常用的继承模式。


4、原型式继承

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 值。


5、寄生式继承

创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。

function createObj (o) {  
    var clone = Object.create(o);  
    clone.sayName = function () {  
        console.log('hi');  
    }  
    return clone;  
} 

缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。


6、寄生组合式继承

为了方便大家阅读,在这里重复一下组合继承的代码

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。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。

原文 http://developer.51cto.com/art/202004/615510.htm

链接: https://www.fly63.com/article/detial/8790

js原型继承、构造函数继承、组合继承法

原型继承缺点:1、不能由子对象像父对象传递参数,2、对于引用型的属性修改之后会印象其他的实例对象;构造函数继承缺点:1、不能继承父对象原型上的方法 2、每次实例化对象会重新构建函数,浪费内存。

各种实现js继承的方法总结

学过java的同学应该都知道,继承是java的重要特点之一,许多面向对象的语言都支持两种继承方式:接口继承和实现继承,接口继承只继承方法签名,而实现继承则继承实际的方法,在js中,由于函数没有签名,因此支持实现继承,而实现继承主要是依靠原型链来实现的,那么,什么是原型链呢?

css的继承性

在面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点:继承了父类的属性和方法。那么我们现在主要研究css,css就是在设置属性的。不会牵扯到方法的层面。

js原型继承的几种方式

1. 原型链继承;2,构造函数继承(对象冒充继承);3,组合继承(原型链继承+构造函数继承);4,原型式继承;5. 寄生组合式继承,为了解决引用共享和超类型无法传参的问题,我们采用一种叫借用构造函数的技术

Js继承背后的场景-prototype,__proto__, [[prototype]]

prototype是构造函数的一个属性,它决定了在构造出来的对象上__proto__属性将是什么样的。如上图所示,理解JavaScript中的继承的关键是要理解母鸡如何产蛋的过程。

Js常用的继承方式

JavaScript常用继承方式主要分为(7种):原型链继承、构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合继承以及继承多个对象。原型链继承(核心:将父类的实例作为子类的原型

JS之继承(ES5 & ES6)

继承到底是什么?继承(英语:inheritance)是面向对象软件技术当中的一个概念。如果一个类别B“继承自”另一个类别A,就把这个B称为“A的子类”,而把A称为“B的父类别”也可以称“A是B的超类”。继承可以使得子类具有父类别的各种属性和方法

Js继承总结

当读取实例的属性时,如果找不到,就会查找与对象关联的原型中的属性,如果还查不到,就去找原型的原型,一直找到最顶层为止。如果让原型对象指向另一个类型的实例.....有趣的事情便发生了.

ES5 的构造函数原型链继承

构造函数,就是专门用来生成实例对象的函数。一个构造函数,可以生成多个实例对象,这些实例对象都有相同的结构。为了与普通函数区别,构造函数名字的第一个字母通常大写。

Js对象继承

JavaScript对象继承的方法有很多,这里总结一下几种比较常用的方法。使用call/apply和Object.create()第一种方法使用call或apply方法,改变了 this 的指向而实现继承,将父对象的构造函数绑定在子对象上,即在子对象构造函数中加一行

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!