javascript 中的继承主要通过原型链(prototype chain)来实现,核心是利用构造函数的 prototype 属性指向一个共享对象,并将该对象赋值给子类的 prototype。具体做法是定义父类构造函数,设置其原型方法,然后通过 object.create() 或 call() 建立父子关系,子类实例即可访问父类属性和方法。若需支持 es6 语法,可直接使用 class 关键字配合 extends 和 super() 实现更直观的继承结构。

基于原型的经典继承如何实现
通过构造函数结合原型链可以实现经典的 JavaScript 继承模式。主要步骤包括:定义父类构造函数并初始化实例属性;在父类原型上添加共享方法;创建子类构造函数,调用父类构造函数以继承实例属性;将子类原型设置为父类实例或 Object.create(父类.prototype) 以继承方法;修正子类原型的 constructor 指向。这种方式完全基于 ES5 及更早版本的语法,是理解 JS 继承机制的基础。

// 父类定义
function Animal(name) {
this.name = name;
this.colors = ['black', 'white'];
}
Animal.prototype.sayName = function() {
console.log(this.name);
};
// 子类定义
function Dog(name, breed) {
// 继承实例属性
Animal.call(this, name);
this.breed = breed;
}
// 继承原型方法
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
// 子类特有方法
Dog.prototype.sayBreed = function() {
console.log(this.breed);
};
// 测试
var dog = new Dog('Fido', 'Golden Retriever');
dog.sayName(); // 输出: Fido
dog.sayBreed(); // 输出: Golden Retriever
console.log(dog instanceof Animal); // true
console.log(dog instanceof Dog); // true
上述代码展示了完整的原型链继承过程。Animal.call(this, name) 确保了每个 Dog 实例拥有独立的 name 和 colors 属性,避免了引用类型属性的共享问题。通过 Object.create(Animal.prototype) 设置 Dog.prototype,使得 Dog 实例可以访问 Animal.prototype 上的 sayName 方法。最后修正 constructor 属性以保持类型标识的准确性。这种模式解决了构造函数继承无法复用方法的缺点,也弥补了纯原型继承导致实例属性共享的缺陷。
立即学习“Java免费学习笔记(深入)”;
ES6 Class 语法的继承机制是什么
ES6 引入了 class 关键字,使继承语法更加简洁直观,但其底层依然基于原型链。实现方式是在子类中使用 extends 关键字指定父类,并在子类构造函数中必须调用 super() 来执行父类构造函数并获取 this 上下文。子类可以通过 override 方法重写父类方法,并使用 super.method() 调用父类被重写的方法。这种语法糖降低了继承的使用门槛,同时保持了与旧版 JavaScript 引擎的兼容性。

// 父类定义
class Animal {
constructor(name) {
this.name = name;
this.colors = ['black', 'white'];
}
sayName() {
console.log(this.name);
}
}
// 子类定义
class Dog extends Animal {
constructor(name, breed) {
super(name); // 调用父类构造函数
this.breed = breed;
}
// 重写父类方法
sayName() {
console.log(`My name is ${this.name}`);
}
// 子类特有方法
sayBreed() {
console.log(this.breed);
}
// 调用父类被重写的方法
callSuperSayName() {
super.sayName();
}
}
// 测试
var dog = new Dog('Fido', 'Golden Retriever');
dog.sayName(); // 输出: My name is Fido
dog.callSuperSayName(); // 输出: Fido
dog.sayBreed(); // 输出: Golden Retriever
console.log(dog instanceof Animal); // true
console.log(dog instanceof Dog); // true
在 class 语法中,extends 自动建立了原型链连接,无需手动设置 prototype。super() 不仅调用了父类构造函数,还隐式地完成了 this 的绑定,这在子类构造函数中是必须的,否则访问 this 会抛出引用错误。super.sayName() 允许在子类方法中直接调用父类同名方法,实现了方法的重用和扩展。这种写法逻辑清晰,易于维护,是现代 JavaScript 开发中推荐使用的继承方式。
当子类重写父类方法时如何调用父类版本
在子类中重写父类方法后,若需保留并调用父类的原始行为,应使用 super.methodName() 语法。这适用于需要扩展而非完全替换父类功能的场景,例如在子类方法中先执行父类逻辑,再添加额外处理。在 ES5 原型模式中,通常通过保存父类方法引用或使用闭包实现类似效果,但 ES6 class 提供了更直接的 super 关键字支持,简化了调用流程并确保上下文正确。
// 示例:在子类中调用父类方法
class Parent {
greet() {
return 'Hello from Parent';
}
}
class Child extends Parent {
greet() {
// 调用父类 greet 方法
const parentGreeting = super.greet();
return `${parentGreeting}, and Hello from Child`;
}
}
var child = new Child();
console.log(child.greet()); // 输出: Hello from Parent, and Hello from Child
此示例展示了 super.greet() 如何准确调用父类 greet 方法,并返回其结果。子类方法可以先获取父类返回值,再进行修改或组合,从而实现行为的增量扩展。这种方式避免了重复编写父类逻辑,提高了代码的可读性和可维护性。需要注意的是,super 只能在类的方法内部使用,且必须在 constructor 中调用 super() 后才能使用 super 访问父类方法。


















