当前位置:首页 > JavaScript

继承的实现js

2026-03-16 01:12:03JavaScript

原型链继承

通过原型链实现继承是 JavaScript 中最基本的继承方式。子类的原型对象指向父类的实例,从而继承父类的属性和方法。

function Parent() {
  this.name = 'Parent';
}

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

function Child() {
  this.childName = 'Child';
}

// 继承实现
Child.prototype = new Parent();

const child = new Child();
child.sayName(); // 输出: Parent

构造函数继承

使用 call 或 apply 方法在子类构造函数中调用父类构造函数,实现属性继承。这种方法无法继承父类原型上的方法。

function Parent(name) {
  this.name = name;
}

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

const child = new Child('Tom', 10);
console.log(child.name); // 输出: Tom

组合继承

结合原型链继承和构造函数继承的优点,既能继承父类实例属性,也能继承父类原型方法。

继承的实现js

function Parent(name) {
  this.name = name;
}

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

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

Child.prototype = new Parent();
Child.prototype.constructor = Child;

const child = new Child('Tom', 10);
child.sayName(); // 输出: Tom

原型式继承

通过 Object.create() 方法实现继承,创建一个新对象,使用现有对象作为新对象的原型。

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

const child = Object.create(parent);
child.name = 'Child';
child.sayName(); // 输出: Child

寄生式继承

在原型式继承的基础上增强对象,返回增强后的对象。

继承的实现js

function createAnother(original) {
  const clone = Object.create(original);
  clone.sayHello = function() {
    console.log('Hello');
  };
  return clone;
}

const parent = {
  name: 'Parent'
};

const child = createAnother(parent);
child.sayHello(); // 输出: Hello

寄生组合式继承

目前最理想的继承方式,解决了组合继承调用两次父类构造函数的问题。

function inheritPrototype(child, parent) {
  const prototype = Object.create(parent.prototype);
  prototype.constructor = child;
  child.prototype = prototype;
}

function Parent(name) {
  this.name = name;
}

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

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

inheritPrototype(Child, Parent);

const child = new Child('Tom', 10);
child.sayName(); // 输出: Tom

ES6 Class 继承

ES6 引入了 class 语法糖,使用 extends 关键字实现继承更加简洁。

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

  sayName() {
    console.log(this.name);
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name);
    this.age = age;
  }
}

const child = new Child('Tom', 10);
child.sayName(); // 输出: Tom

标签: js
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js节流实现

js节流实现

节流的概念 节流(Throttle)是一种限制函数执行频率的技术,确保函数在一定时间间隔内只执行一次。常用于滚动事件、窗口调整等高频触发的场景。 基础实现方法 使用时间戳判断是否执行函数: fun…

js实现排序

js实现排序

数组排序方法 JavaScript提供了内置的sort()方法用于数组排序。默认情况下,sort()将元素转换为字符串并按照Unicode码点排序。对于数字排序,需传入比较函数。 const num…

js实现复制功能实现

js实现复制功能实现

使用document.execCommand方法(传统方式,已逐渐被弃用) 传统方式通过document.execCommand('copy')实现复制功能,需先选中文本。以下是一个完整示例: fu…

js 多选 实现

js 多选 实现

实现多选的 JavaScript 方法 使用复选框(Checkbox)实现多选 通过 HTML 的 checkbox 元素配合 JavaScript 实现多选功能。用户可以选择多个选项,提交时获取所有…