当前位置:首页 > JavaScript

实现继承js

2026-04-03 22:15:56JavaScript

实现继承的方式

在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'

组合继承

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

实现继承js

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'

原型式继承

通过一个函数创建一个临时构造函数,将其原型指向传入的对象,返回该构造函数的实例。

function createObject(obj) {
  function F() {}
  F.prototype = obj;
  return new F();
}
const parent = {
  name: 'parent',
  sayName: function() {
    console.log(this.name);
  }
};
const child = createObject(parent);
child.sayName(); // 输出 'parent'

寄生式继承

在原型式继承的基础上增强对象,返回一个经过扩展的对象。

实现继承js

function createAnother(obj) {
  const clone = Object.create(obj);
  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) {
  Parent.call(this, name);
}
inheritPrototype(Child, Parent);
const child = new Child('child');
child.sayName(); // 输出 'child'

ES6 Class继承

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

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'

每种继承方式各有优缺点,选择适合场景的方式实现继承。

标签: js
分享给朋友:

相关文章

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现报表

js实现报表

使用JavaScript实现报表 在JavaScript中实现报表功能可以通过多种方式完成,常见的方法包括使用原生JavaScript、第三方库(如Chart.js、D3.js)或结合后端数据渲染。以…

js实现vr

js实现vr

使用WebXR API实现VR体验 WebXR是浏览器中实现VR和AR体验的标准API,它取代了早期的WebVR。现代浏览器如Chrome、Edge和Firefox已支持WebXR。 // 初始化W…

js实现变形

js实现变形

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

js实现游标

js实现游标

使用JavaScript实现游标 在JavaScript中,可以通过操作DOM元素的cursor样式属性来实现自定义游标效果。以下是几种常见的实现方法: 修改默认鼠标指针样式 通过CSS的curso…

js手势实现

js手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…