如何在JavaScript中实现继承?

来源:个人站长作者:小黄人头衔:程序员
导读:本期聚焦于小伙伴创作的《如何在JavaScript中实现继承?》,敬请观看详情。在JavaScript开发中,继承是实现代码复用和对象关系构建的重要特性。很多开发者刚接触JavaScript时会对继承的实现方式感到困惑,不清楚不同继承方案的差异和适用场景。本文将详细介绍JavaScript中实现继承的多种方式,包括原型链继承、构造函数继承、组合继承、原型式继承、寄生式继承和寄生组合式继承等。会逐一讲解每种方式的实现原理、代码示例,分析其优缺点和适用场景,帮助开发者理解不同继承方案的设计思路,在实际开发中能够根据需求选择最合适的继承实现方式,提升代码的复用性和可维护性。

JavaScript作为一门基于原型的编程语言,本身没有传统面向对象语言的类继承语法,不过可以通过多种手段实现继承效果,满足代码复用和对象关系构建的需求。下面介绍几种常见的JavaScript继承实现方式。

如何在JavaScript中实现继承?

原型链继承

原型链继承是JavaScript中最基础的继承方式,核心是利用原型链让一个引用类型继承另一个引用类型的属性和方法。

// 父类构造函数
function Parent() {
    this.name = 'parent';
    this.colors = ['red', 'blue'];
}
// 父类原型方法
Parent.prototype.getName = function() {
    return this.name;
};
// 子类构造函数
function Child() {
    this.age = 18;
}
// 将子类的原型指向父类的实例,实现原型链继承
Child.prototype = new Parent();
// 修正子类原型的构造函数指向
Child.prototype.constructor = Child;
// 子类原型方法
Child.prototype.getAge = function() {
    return this.age;
};

// 测试
const child1 = new Child();
console.log(child1.getName()); // 输出 parent
console.log(child1.getAge()); // 输出 18
// 引用类型属性被所有实例共享的问题
child1.colors.push('green');
const child2 = new Child();
console.log(child2.colors); // 输出 ["red", "blue", "green"]

这种方式的优点是简单易懂,能继承父类原型上的方法。缺点是父类实例的引用类型属性会被所有子类实例共享,并且创建子类实例时无法向父类构造函数传参。

构造函数继承

构造函数继承也叫经典继承,核心是在子类构造函数中调用父类构造函数,通过callapply改变父类的this指向。

// 父类构造函数
function Parent(name) {
    this.name = name;
    this.colors = ['red', 'blue'];
}
// 父类原型方法
Parent.prototype.getName = function() {
    return this.name;
};
// 子类构造函数
function Child(name, age) {
    // 调用父类构造函数,将this指向当前子类实例
    Parent.call(this, name);
    this.age = age;
}

// 测试
const child1 = new Child('child1', 18);
console.log(child1.name); // 输出 child1
console.log(child1.age); // 输出 18
child1.colors.push('green');
console.log(child1.colors); // 输出 ["red", "blue", "green"]
const child2 = new Child('child2', 20);
console.log(child2.colors); // 输出 ["red", "blue"] 不会互相影响
// 无法继承父类原型上的方法
console.log(child1.getName); // 输出 undefined

这种方式的优点是可以向父类传参,子类实例不会共享父类的引用类型属性。缺点是无法继承父类原型上的方法,方法都在构造函数中定义,无法实现函数复用。

组合继承

组合继承是结合原型链继承和构造函数继承的优点,是最常用的继承方式之一。

// 父类构造函数
function Parent(name) {
    this.name = name;
    this.colors = ['red', 'blue'];
}
// 父类原型方法
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;
// 子类原型方法
Child.prototype.getAge = function() {
    return this.age;
};

// 测试
const child1 = new Child('child1', 18);
console.log(child1.getName()); // 输出 child1
console.log(child1.getAge()); // 输出 18
child1.colors.push('green');
console.log(child1.colors); // 输出 ["red", "blue", "green"]
const child2 = new Child('child2', 20);
console.log(child2.colors); // 输出 ["red", "blue"]
console.log(child2.getName()); // 输出 child2

这种方式的优点是既可以继承父类原型上的方法实现函数复用,又能保证每个子类实例有自己的属性,还能向父类传参。缺点是父类构造函数会被调用两次,一次是在创建子类原型时,一次是在子类构造函数中,存在一定的性能开销。

寄生组合式继承

寄生组合式继承是对组合继承的优化,解决了父类构造函数被调用两次的问题,是业界公认的最优继承方案。

// 父类构造函数
function Parent(name) {
    this.name = name;
    this.colors = ['red', 'blue'];
}
// 父类原型方法
Parent.prototype.getName = function() {
    return this.name;
};
// 寄生组合式继承的核心函数
function inheritPrototype(child, parent) {
    // 创建父类原型的副本
    const prototype = Object.create(parent.prototype);
    // 修正副本的构造函数指向
    prototype.constructor = child;
    // 将子类的原型指向该副本
    child.prototype = prototype;
}
// 子类构造函数
function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
}
// 调用继承函数
inheritPrototype(Child, Parent);
// 子类原型方法
Child.prototype.getAge = function() {
    return this.age;
};

// 测试
const child1 = new Child('child1', 18);
console.log(child1.getName()); // 输出 child1
console.log(child1.getAge()); // 输出 18
console.log(child1 instanceof Child); // 输出 true
console.log(child1 instanceof Parent); // 输出 true

这种方式只调用了一次父类构造函数,并且避免了在子类原型上创建不必要的属性,同时还能保持原型链的正常结构,是使用最多的继承实现方式。

ES6 class继承

ES6引入了class语法,其底层的继承实现本质还是基于原型链,但是语法更加简洁清晰,更接近传统面向对象的继承写法。

// 父类
class Parent {
    constructor(name) {
        this.name = name;
        this.colors = ['red', 'blue'];
    }
    getName() {
        return this.name;
    }
}
// 子类,通过extends关键字继承父类
class Child extends Parent {
    constructor(name, age) {
        // 调用父类构造函数,必须在this之前调用
        super(name);
        this.age = age;
    }
    getAge() {
        return this.age;
    }
}

// 测试
const child1 = new Child('child1', 18);
console.log(child1.getName()); // 输出 child1
console.log(child1.getAge()); // 输出 18
child1.colors.push('green');
console.log(child1.colors); // 输出 ["red", "blue", "green"]
const child2 = new Child('child2', 20);
console.log(child2.colors); // 输出 ["red", "blue"]

ES6的class继承语法糖让继承的写法更加直观,不需要手动处理原型链和构造函数指向,开发中如果使用现代JavaScript语法,优先选择这种方式实现继承。

JavaScript继承原型链构造函数组合继承修改时间:2026-06-06 07:39:16

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。