JavaScript类继承通过extends实现子类复用父类属性方法,基于原型链但用class语法更直观清晰,提升代码可读性与维护性。

JavaScript中的类继承,简单来说,就是一种让一个“子类”能够从一个“父类”那里继承属性和方法的能力。它允许我们构建一个层级结构,让子类在拥有自己独特功能的同时,也能复用父类的通用行为,从而减少代码重复,提高可维护性。在ES6之后,我们有了
class
在我看来,理解JS的类继承,首先得从ES6引入的
class
extends
想象一下,我们有一个通用的
Animal
name
age
eat()
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
eat() {
console.log(`${this.name} is eating.`);
}
sleep() {
console.log(`${this.name} is sleeping.`);
}
}现在,我们想创建一个
Dog
bark()
Dog
Animal
class Dog extends Animal {
constructor(name, age, breed) {
// 关键点:在子类构造函数中,必须先调用super()。
// super()调用的是父类(Animal)的构造函数,负责初始化父类部分的属性。
super(name, age);
this.breed = breed; // Dog类特有的属性
}
bark() {
console.log(`${this.name} barks: Woof! Woof!`);
}
// 我们可以覆盖父类的方法
eat() {
console.log(`${this.name} is happily munching on kibble.`);
}
// 也可以在覆盖的同时调用父类的方法
// sayHello() {
// super.sayHello(); // 调用父类的sayHello方法
// console.log(`My breed is ${this.breed}.`);
// }
}
const myDog = new Dog('Buddy', 3, 'Golden Retriever');
myDog.eat(); // 输出:Buddy is happily munching on kibble. (调用子类覆盖的方法)
myDog.sleep(); // 输出:Buddy is sleeping. (调用父类继承的方法)
myDog.bark(); // 输出:Buddy barks: Woof! Woof! (调用子类特有的方法)
console.log(myDog.name); // Buddy
console.log(myDog.age); // 3
console.log(myDog.breed); // Golden Retriever这里面有几个非常核心的要点:
extends
Dog
Animal
super()
constructor
super()
this
super()
name
age
constructor
super()
super.methodName()
总的来说,JS的类继承提供了一种结构化的方式来组织代码,让我们的程序逻辑更加清晰,尤其是在处理具有层级关系的对象时,它显得尤为重要。
在我个人的开发经历中,ES6的
class
ES6引入
class
class
constructor
extends
super
prototype
Object.create
call
apply
class
class
import
export
super
this
super
class
super()
super.methodName()
this
class
static
static
尽管
class
在日常的JavaScript开发中,类继承的应用非常广泛,尤其是在构建大型、复杂的应用时。但同时,我们也需要注意一些最佳实践,避免过度设计或引入不必要的复杂性。
常见的应用场景:
BaseComponent
render
mount
unmount
Button
Input
Modal
BaseComponent
Error
NetworkError
AuthenticationError
ValidationError
class NetworkError extends Error {
constructor(message, status) {
super(message);
this.name = 'NetworkError';
this.status = status;
}
}
try {
// 模拟网络请求失败
throw new NetworkError('Failed to fetch data', 500);
} catch (error) {
if (error instanceof NetworkError) {
console.error(`Status ${error.status}: ${error.message}`);
} else {
console.error('An unexpected error occurred:', error.message);
}
}catch
instanceof
最佳实践:
Car
Engine
Car
Engine
super
super
super()
this
继承是一个强大的工具,但像所有工具一样,它需要被正确地使用。在实践中,我们应该根据具体的需求和设计原则,权衡继承与组合的优劣,选择最适合的方案。
本文档主要讲述的是android界面布局详解;在通过“Hello World!”介绍Android中的布局问题之前,不得不先介绍一下Android中的用户界面,因为布局问题也是用户界面问题之一。在一个Android应用程序中,用户界面通过View和ViewGroup对象构建。Android中有很多种Views和ViewGroups,他们都继承自View类。View对象是Android平台上表示用户界面的基本单元。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过
1
要深入理解JS的类继承,就不能绕开它与原型链继承的关系。我个人认为,它们的本质联系远大于区别,而所谓的“区别”更多体现在语法糖和开发者体验上。
本质联系:
class
class
class
extends
[[Prototype]]
__proto__
class Dog extends Animal {}Object.setPrototypeOf(Dog.prototype, Animal.prototype)
Dog
Animal.prototype
Dog
Animal
Object.setPrototypeOf(Dog, Animal)
主要区别(体现在语法、开发者体验和一些细节行为):
语法层面:
原型链继承(ES5及以前):通常需要手动操作
prototype
function Animal(name) {
this.name = name;
}
Animal.prototype.eat = function() { console.log(`${this.name} eats.`); };
function Dog(name, breed) {
Animal.call(this, name); // 构造函数继承
this.breed = breed;
}
// 原型链继承
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog; // 修复constructor指向
Dog.prototype.bark = function() { console.log(`${this.name} barks.`); };这种方式相对冗长,且需要注意
constructor
类继承(ES6 class
class
extends
class Animal { /* ... */ }
class Dog extends Animal { /* ... */ }super
super
super()
super.methodName()
this
super
Parent.call(this, ...)
Parent.prototype.methodName.call(this, ...)
this
构造函数行为:
super()
this
super()
this
this
Parent.call(this, ...)
this
严格模式:
class
'use strict'
不可枚举的方法:
class
enumerable: false
Function.prototype.method = function() {}归根结底,它们都是JavaScript实现面向对象继承的方式。
class
以上就是什么是JS的类继承?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号