欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript之继承的多种方式及优缺点介绍

程序员文章站 2022-08-26 11:19:06
原型链继承 借用构造函数(经典继承) 组合继承 原型式继承 寄生式继承 寄生组合式继承 1.原型链继承 function parent () { this.n...

原型链继承 借用构造函数(经典继承) 组合继承 原型式继承 寄生式继承 寄生组合式继承

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();

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);

这种方式的高效率体现它只调用了一次 parent 构造函数,并且因此避免了在 parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isprototypeof。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。