当前位置:首页 > JavaScript

js实现extends

2026-04-05 20:04:28JavaScript

实现继承的方式

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

原型链继承

原型链继承通过将子类的原型指向父类的实例来实现继承。

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

Parent.prototype.getName = function() {
  return this.name;
};

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

Child.prototype = new Parent();

const child = new Child();
console.log(child.getName()); // 输出 'Parent'

构造函数继承

构造函数继承通过在子类构造函数中调用父类构造函数来实现继承。

js实现extends

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

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

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

组合继承

组合继承结合了原型链继承和构造函数继承的优点。

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

Parent.prototype.getName = function() {
  return this.name;
};

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

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

const child = new Child('Parent', 'Child');
console.log(child.getName()); // 输出 'Parent'

原型式继承

原型式继承通过创建一个临时构造函数来实现继承。

js实现extends

function createObject(obj) {
  function F() {}
  F.prototype = obj;
  return new F();
}

const parent = {
  name: 'Parent',
  getName: function() {
    return this.name;
  }
};

const child = createObject(parent);
console.log(child.getName()); // 输出 'Parent'

寄生式继承

寄生式继承在原型式继承的基础上增强了对象。

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

const parent = {
  name: 'Parent',
  getName: function() {
    return this.name;
  }
};

const child = createObject(parent);
console.log(child.getName()); // 输出 '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.getName = function() {
  return this.name;
};

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

inheritPrototype(Child, Parent);

const child = new Child('Parent', 'Child');
console.log(child.getName()); // 输出 'Parent'

ES6 Class 继承

ES6 引入了 classextends 关键字,使继承更加简洁。

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

  getName() {
    return this.name;
  }
}

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

const child = new Child('Parent', 'Child');
console.log(child.getName()); // 输出 'Parent'

选择继承方式的建议

  • 如果需要简单的继承,可以使用原型链继承或构造函数继承。
  • 如果需要更复杂的继承,推荐使用组合继承或寄生组合式继承。
  • 在现代 JavaScript 开发中,优先使用 ES6 的 classextends 语法。

以上方法各有优缺点,根据具体需求选择合适的继承方式。

标签: jsextends
分享给朋友:

相关文章

vue.js实现轮播

vue.js实现轮播

vue.js实现轮播的方法 使用第三方库(如Swiper) 安装Swiper库: npm install swiper 在Vue组件中引入Swiper: <template> &l…

js实现vue

js实现vue

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

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(f…