当前位置:首页 > 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
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Par…

js 实现vue

js 实现vue

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

js实现tab选项卡切换

js实现tab选项卡切换

实现Tab选项卡切换的JavaScript方法 使用纯JavaScript实现Tab切换功能,可以通过监听点击事件动态切换内容显示状态。以下是两种常见实现方式: 基础DOM操作实现 // 获取所有…

js实现变形

js实现变形

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

js画图实现

js画图实现

使用Canvas API绘制图形 Canvas是HTML5提供的绘图API,通过JavaScript操作Canvas元素可以绘制各种图形。以下是一个简单的示例: <canvas id="myC…