javascript中的继承如何实现_有哪些方法?

JavaScript继承主要通过原型链实现,ES6前无class语法,ES6引入class/extends语法糖但底层仍基于原型链和构造函数,常见方式有原型链继承、构造函数继承、组合继承和ES6 class继承。

JavaScript 中的继承主要通过原型链实现,没有传统面向对象语言中的 class 继承语法(ES6 之前),但 ES6 引入了 classextends 语法糖,底层仍是基于原型链和构造函数。常见实现方式有以下几种:

1. 原型链继承

让子构造函数的 prototype 指向父构造函数的一个实例。

优点:简单,能复用父类方法;
缺点:所有子实例共享父类实例的引用属性,无法向父构造函数传参。

示例:

function Animal(name) {
  this.name = name;
  this.colors = ['black', 'white'];
}
Animal.prototype.say = function() {
  return `I'm ${this.name}`;
};

function Dog() {}
Dog.prototype = new Animal(); // 关键:继承原型
Dog.prototype.constructor = Dog;

const d1 = new Dog();
const d2 = new Dog();
d1.colors.push('brown');
console.log(d2.colors); // ['black', 'white', 'brown'] ← 共享了引用类型

2. 构造函数继承(借用构造函数)

在子构造函数中用 callapply 调用父构造函数。

优点:避免引用属性共享,支持传参;
缺点:只能继承实例属性/方法,无法复用父类原型上的方法。

示例:

function Animal(name) {
  this.name = name;
  this.colors = ['black', 'white'];
}
Animal.prototype.say = function() {
  return `I'm ${this.name}`;
};

function Dog(name, breed) {
  Animal.call(this, name); // 关键:继承实例属性
  this.breed = breed;
}

const d = new Dog('Xiaohei', 'Shepherd');
console.log(d.name); // 'Xiaohei'
console.log(d.say); // undefined ← 原型方法没被继承

3. 组合继承(最常用)

结合前两种:原型链继承方法 + 构造函数继承属性。

优点:兼顾属性独立与方法复用;
缺点:父构造函数被调用两次(一次在设置原型时,一次在子构造函数内)。

示例:

function Animal(name) {
  this.name = name;
  this.colors = ['black', 'white'];
}
Animal.prototype.say = function() {
  return `I'm ${this.name}`;
};

function Dog(name, breed) {
  Animal.call(this, name); // 第一次调用
  this.breed = breed;
}
Dog.prototype = new Animal(); // 第二次调用 ← 可优化
Dog.prototype.constructor = Dog;

const d = new Dog('Wangcai', 'Poodle');
console.log(d.say()); // 'I'm Wangcai'
console.log(d.colors); // ['black', 'white'](不共享)

4. ES6 class 继承(推荐现代写法)

本质是组合继承的语法糖,内部自动处理原型和构造调用,更简洁安全。

关键点:
– 必须在子类 constructor 中调用 super()
super 既可调用父类构造函数,也可访问父类原型方法;
– 支持 static 方法、get/setnew.target 等。

示例:

class Animal {
  constructor(name) {
    this.name = name;
    this.colors = ['black', 'white'];
  }
  say() {
    return `I'm ${this.name}`;
  }
  static info() {
    return 'Animal base class';
  }
}

class Dog extends Animal {
  constructor(name, breed) {
    super(name); // ← 必须先调用,初始化 this
    this.breed = breed;
  }
  bark() {
    return `${this.say()} and I bark!`;
  }
}

const d = new Dog('Husky', 'Siberian');
console.log(d.bark()); // 'I'm Husky and I bark!'
console.log(Dog.info()); // 'Animal base class'

基本上就这些。日常开发优先用 class + extends,清晰、语义强、工具友好;理解原型链继承有助于调试和深入掌握 JS 机制。