当前位置:首页 > JavaScript

js 实现面向对象

2026-03-01 02:21:51JavaScript

原型链继承

利用 JavaScript 的原型链机制实现继承。通过将子类的原型指向父类的实例,子类可以访问父类的属性和方法。

function Parent() {
  this.name = 'Parent';
}
Parent.prototype.sayName = function() {
  console.log(this.name);
};

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

const child = new Child();
child.sayName(); // 输出 "Child"

构造函数继承

在子类构造函数中调用父类构造函数,使用 call 或 apply 方法改变 this 指向,实现属性继承。

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;
}

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

组合继承

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

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('Child', 10);
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 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('Child', 10);
child.sayName(); // 输出 "Child"

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('Child', 10);
child.sayName(); // 输出 "Child"

标签: 面向对象js
分享给朋友:

相关文章

js实现tab选项卡切换

js实现tab选项卡切换

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

js实现 功能

js实现 功能

在 JavaScript 中实现功能通常涉及多个关键步骤,具体取决于功能需求。以下是常见功能的实现方法和示例: 基本功能实现 使用函数封装逻辑是最直接的方式。例如,实现一个计算两数之和的函数: f…

js验证码的实现

js验证码的实现

验证码的基本实现原理 验证码(CAPTCHA)的核心目标是区分人类用户和自动化程序。JavaScript可用于生成或验证客户端验证码,但需注意纯前端验证可能被绕过,通常需结合后端验证。 纯前端验证码…

js实现上传图片

js实现上传图片

使用HTML5的File API实现图片上传 HTML5的File API允许通过JavaScript访问用户选择的文件。需要创建一个文件输入元素,并监听其change事件。 <input t…

js实现点击隐藏

js实现点击隐藏

使用CSS类切换实现点击隐藏 通过添加或移除CSS类来控制元素的显示与隐藏。定义一个隐藏样式类如.hidden { display: none; },通过事件监听切换该类。 document.que…

vue js 实现排序

vue js 实现排序

Vue.js 实现排序的方法 在 Vue.js 中实现排序可以通过多种方式完成,包括使用计算属性、方法或直接操作数据。以下是几种常见的实现方式: 使用计算属性排序 计算属性可以自动响应数据变化并重新…