当前位置:首页 > JavaScript

js 实现面向对象

2026-04-04 02:54:37JavaScript

原型链继承

利用原型链实现继承是 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'

构造函数继承

在子类型构造函数的内部调用超类型构造函数,通过使用 apply() 或 call() 方法在新创建的对象上执行构造函数。

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

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

const child = new Child('child');
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', 18);
child.sayName(); // 输出 'child'
console.log(child.age); // 输出 18

原型式继承

借助原型基于已有对象创建新对象,同时不必创建自定义类型。

function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}

const parent = {
  name: 'parent',
  sayName: function() {
    console.log(this.name);
  }
};

const child = object(parent);
child.name = 'child';
child.sayName(); // 输出 'child'

寄生式继承

创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后返回对象。

function createAnother(original) {
  const clone = Object.create(original);
  clone.sayHi = function() {
    console.log('hi');
  };
  return clone;
}

const parent = {
  name: 'parent',
  sayName: function() {
    console.log(this.name);
  }
};

const child = createAnother(parent);
child.sayName(); // 输出 'parent'
child.sayHi(); // 输出 'hi'

寄生组合式继承

通过借用构造函数来继承属性,通过原型链的混成形式来继承方法,不必为了指定子类型的原型而调用超类型的构造函数。

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', 18);
child.sayName(); // 输出 'child'
console.log(child.age); // 输出 18

ES6 Class 继承

ES6 引入了 class 语法糖,使得面向对象编程更加直观和易于理解。

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', 18);
child.sayName(); // 输出 'child'
console.log(child.age); // 输出 18

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

相关文章

js实现密码

js实现密码

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

js验证码的实现

js验证码的实现

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

js实现下拉刷新

js实现下拉刷新

监听触摸事件 通过监听 touchstart、touchmove 和 touchend 事件来检测用户下拉手势。记录触摸起始位置和移动距离。 let startY = 0; let currentY…

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculato…

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…

js实现图片移动

js实现图片移动

使用CSS和JavaScript实现图片移动 方法一:使用CSS动画结合JavaScript控制 通过CSS定义动画关键帧,JavaScript动态添加或移除动画类。 /* CSS部分 */ .m…