当前位置:首页 > JavaScript

js实现继承怎么实现的

2026-03-15 12:43:26JavaScript

在JavaScript中,继承可以通过多种方式实现,以下是常见的几种方法:

原型链继承

通过将子类的原型指向父类的实例实现继承。子类可以访问父类原型上的属性和方法。

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

function Child() {}
Child.prototype = new Parent();

const child = new Child();
child.sayName(); // 'parent'

构造函数继承

在子类构造函数中调用父类构造函数,通过callapply改变this指向。子类可以继承父类实例属性,但无法继承父类原型上的方法。

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

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

const child = new Child('child');
console.log(child.name); // 'child'
child.sayName(); // Error: child.sayName is not a function

组合继承

结合原型链继承和构造函数继承的优点。通过构造函数继承实例属性,通过原型链继承原型方法。

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

function Child(name) {
  Parent.call(this, name);
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;

const child = new Child('child');
child.sayName(); // 'child'

原型式继承

基于已有对象创建新对象,无需定义构造函数。使用Object.create()方法实现。

const parent = {
  name: 'parent',
  sayName: function() {
    console.log(this.name);
  }
};

const child = Object.create(parent);
child.name = 'child';
child.sayName(); // 'child'

寄生式继承

在原型式继承的基础上增强对象,添加额外方法或属性。

function createChild(parent) {
  const child = Object.create(parent);
  child.sayHello = function() {
    console.log('Hello');
  };
  return child;
}

const parent = { name: 'parent' };
const child = createChild(parent);
child.sayHello(); // 'Hello'

寄生组合式继承

最优的继承方式,避免组合继承中两次调用父类构造函数的问题。通过Object.create()复制父类原型,减少不必要的属性继承。

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

function Child(name) {
  Parent.call(this, name);
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

const child = new Child('child');
child.sayName(); // 'child'

ES6 Class继承

使用classextends关键字实现继承,语法更简洁直观。

js实现继承怎么实现的

class Parent {
  constructor(name) {
    this.name = name;
  }
  sayName() {
    console.log(this.name);
  }
}

class Child extends Parent {
  constructor(name) {
    super(name);
  }
}

const child = new Child('child');
child.sayName(); // 'child'

每种继承方式各有优缺点,应根据具体需求选择合适的方法。ES6 Class继承是目前推荐的方式,语法简洁且功能完善。

标签: js
分享给朋友:

相关文章

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML结…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…