当前位置:首页 > 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"

组合继承

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

js 实现面向对象

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"

寄生式继承

在原型式继承的基础上增强对象,返回新对象。

js 实现面向对象

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 关键字实现继承,语法更简洁。

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实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的JWT令牌…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js图片轮播的实现

js图片轮播的实现

基础图片轮播实现 使用HTML、CSS和JavaScript实现一个简单的图片轮播效果。HTML部分定义轮播容器和图片,CSS负责样式和过渡效果,JavaScript处理逻辑。 <div cl…

js实现图片滚动

js实现图片滚动

图片滚动的实现方法 使用CSS动画实现 通过CSS的animation和@keyframes可以实现简单的图片滚动效果。这种方法适合静态图片的无缝循环滚动。 <style> .scr…