当前位置:首页 > 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

组合继承

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

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

寄生式继承

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

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 关键字实现继承更加简洁。

继承的实现js

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实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…

js实现dh

js实现dh

JavaScript 实现 DH(Diffie-Hellman)密钥交换 Diffie-Hellman(DH)密钥交换是一种安全协议,允许双方在不安全的通信信道上建立一个共享密钥。以下是如何在 Jav…

js类实现

js类实现

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

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…

js进度条实现

js进度条实现

使用HTML和CSS创建基础结构 在HTML中创建一个容器元素用于显示进度条,通常使用<div>元素。CSS用于设置进度条的样式,包括宽度、高度、颜色和圆角等属性。 <div cl…

js实现显示隐藏

js实现显示隐藏

显示隐藏的实现方法 在JavaScript中,实现元素的显示和隐藏可以通过多种方式完成。以下是几种常见的方法: 修改CSS的display属性 通过改变元素的display属性可以在none(隐藏)…