当前位置:首页 > JavaScript

怎样实现js的继承

2026-01-31 02:29:44JavaScript

在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方法实现继承。这种方式可以继承父类的实例属性,但无法继承原型上的方法。

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(); // 报错,无法访问父类原型方法

组合继承

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

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',
  sayName: function() {
    console.log(this.name);
  }
};

const child = createChild(parent);
child.sayName(); // 输出 'Parent'
child.sayHello(); // 输出 'Hello'

寄生组合式继承

结合组合继承和寄生式继承的优点,避免重复调用父类构造函数,是最理想的继承方式。

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类继承

使用ES6的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类继承是目前最推荐的方式,语法简洁且功能强大。

标签: js
分享给朋友:

相关文章

js实现论坛

js实现论坛

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

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js类实现

js类实现

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

js实现密码

js实现密码

密码强度验证 使用正则表达式验证密码强度是一种常见方法。以下代码检查密码是否包含大小写字母、数字和特殊字符,且长度至少为8位: function checkPasswordStrength(pass…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: functio…