JavaScript如何实现继承的

PHPz
发布: 2023-04-06 10:10:36
原创
1039 人浏览过

JavaScript是一种面向对象的语言,而继承是面向对象编程的一个重要特性。在JavaScript中,继承的实现方式有多种,本文将介绍其中较为常见的几种方法。

一、原型链继承

原型链继承是JavaScript中最基本的一种继承方式,也是最常用的一种。通过原型链继承,可以实现子类继承父类的属性和方法。

原型链继承的实现方法如下:

function Parent(name) {
    this.name = name;
    this.colors = ['red', 'green', 'blue'];
}

Parent.prototype.sayName = function() {
    console.log(this.name);
};

function Child(name, age) {
    this.age = age;
    Parent.call(this, name);
}

Child.prototype = new Parent();
Child.prototype.constructor = Child;

var child1 = new Child('Tom', 18);
child1.colors.push('black');
console.log(child1.colors);  // ['red', 'green', 'blue', 'black']
child1.sayName();  // Tom

var child2 = new Child('Jerry', 20);    
console.log(child2.colors);  // ['red', 'green', 'blue']
child2.sayName();  // Jerry
登录后复制

上述代码中,我们首先定义了一个父类Parent和一个子类Child。在Child中,我们使用Parent.call(this, name)调用了父类构造函数,并通过Child.prototype = new Parent()将Child的原型指向了Parent,从而实现了继承。在最后,我们创建了两个子类实例child1和child2,并验证了继承的效果。

原型链继承的优点是实现简单,容易理解。但它的缺点也很明显,即会造成原型对象中的引用类型属性被所有实例共享。

二、构造函数继承

构造函数继承是一种比较容易理解的继承方式,它通过在子类构造函数中调用父类构造函数实现继承。

构造函数继承的实现方法如下:

function Parent(name) {
    this.name = name;
    this.colors = ['red', 'green', 'blue'];
}

Parent.prototype.sayName = function() {
    console.log(this.name);
};

function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
}

var child1 = new Child('Tom', 18);
child1.colors.push('black');
console.log(child1.colors);  // ['red', 'green', 'blue', 'black']
child1.sayName();  // TypeError: child1.sayName is not a function 

var child2 = new Child('Jerry', 20);    
console.log(child2.colors);  // ['red', 'green', 'blue']
child2.sayName();  // TypeError: child2.sayName is not a function
登录后复制

上述代码中,我们在子类构造函数Child中使用Parent.call(this, name)调用了父类构造函数,并将this指向子类实例,从而实现了继承。但由于构造函数继承并不会将父类原型中的方法继承下来,因此我们在子类中无法直接调用父类原型中的方法。

构造函数继承的优点是避免了原型链继承中引用类型属性被所有实例共享的问题,但它的缺点也很明显,即无法继承父类原型中的方法。

三、组合继承

组合继承是JavaScript中最常用的一种继承方式,它是将原型链继承和构造函数继承结合起来的一种方式,解决了两者各自的缺点。

组合继承的实现方法如下:

function Parent(name) {
    this.name = name;
    this.colors = ['red', 'green', 'blue'];
}

Parent.prototype.sayName = function() {
    console.log(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('Tom', 18);
child1.colors.push('black');
console.log(child1.colors);  // ['red', 'green', 'blue', 'black']
child1.sayName();  // Tom

var child2 = new Child('Jerry', 20);    
console.log(child2.colors);  // ['red', 'green', 'blue']
child2.sayName();  // Jerry
登录后复制

上述代码中,我们在子类构造函数Child中使用Parent.call(this, name)调用了父类构造函数,并将this指向子类实例,从而实现了继承。同时,我们在子类原型中通过Child.prototype = new Parent()使子类继承了父类的原型,从而继承了父类原型中的方法。

组合继承的优点是继承方式完整,既可以继承父类原型中的方法,又可以避免原型链继承中引用类型属性被所有实例共享的问题。但它的缺点是会调用两次父类构造函数,浪费了一定的内存空间。

四、寄生组合继承

寄生组合继承是组合继承的一种优化方式,它避免了在子类原型中多次调用父类构造函数的问题,从而减少了内存开销。

寄生组合继承的实现方法如下:

function Parent(name) {
    this.name = name;
    this.colors = ['red', 'green', 'blue'];
}

Parent.prototype.sayName = function() {
    console.log(this.name);
};

function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
}

Child.prototype = Object.create(Parent.prototype, {
    constructor: {
        value: Child,
        enumerable: false,
        writable: true,
        configurable: true
    }
});

var child1 = new Child('Tom', 18);
child1.colors.push('black');
console.log(child1.colors);  // ['red', 'green', 'blue', 'black']
child1.sayName();  // Tom

var child2 = new Child('Jerry', 20);    
console.log(child2.colors);  // ['red', 'green', 'blue']
child2.sayName();  // Jerry
登录后复制

上述代码中,我们在子类原型中使用Object.create()方法创建了一个父类原型的副本,并通过Object.create()的第二个参数重写了子类原型的constructor属性,从而使其指向了子类自身。由于Object.create()方法并不会调用父类构造函数,因此就避免了在子类原型中多次调用父类构造函数的问题。

寄生组合继承的优点是既继承了父类原型中的方法,又避免了在子类原型中多次调用父类构造函数的问题。缺点是实现较为复杂。

总之,JavaScript中实现继承的方法有很多种,每一种方法都有各自的优缺点。在实际开发中,我们应该根据具体情况选择合适的继承方式。

以上是JavaScript如何实现继承的的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!