封装_私有类字段和方法

前言

在 JavaScript 中,封装、私有类字段和方法是面向对象编程的一种重要特性。它们允许你将数据(属性)和对数据的操作(方法)组合在一起,并控制访问权限,从而提高代码的安全性和可维护性。私有字段和私有方法是指仅能在类的内部访问的属性和函数。外部代码无法直接访问或修改这些私有成员,这样就可以防止外部依赖于内部实现细节,提高了模块化和安全性。
本篇文章我们就来学习一下JavaScript中的公有字段,私有字段,公有方法,私有方法,还是以上一节的代码举例

javascript 复制代码
class Account {
  constructor(owner, currency, pin) {
    this.owner = owner;
    this.currency = currency;
    this._pin = pin;
    this._movements = [];
    this.locale = navigator.language;

    console.log(`欢迎来到你的账户,${owner}`);
  }

  //公共接口
  getMovements() {
    return this._movements;
  }

  deposit(val) {
    this._movements.push(val);
  }

  withraw(val) {
    this.deposit(-val);
  }

  _approveLoan(val) {
    return true;
  }

  requestLoan(val) {
    if (this._approveLoan(val)) {
      this.deposit(val);
      console.log('恭喜你!贷款成功');
    }
  }
}

公共字段

● 例如我们将浏览器设置语言和存取款的操作记录设置我公有字段,所有人都可以访问

javascript 复制代码
class Account {
  //公有字段(例子,实际与之前写法作用一样)
  locale = navigator.language;
  _movements = [];

  constructor(owner, currency, pin) {
    this.owner = owner;
    this.currency = currency;
    this._pin = pin;

    console.log(`欢迎来到你的账户,${owner}`);
  }

  //公共接口
  getMovements() {
    return this._movements;
  }

  deposit(val) {
    this._movements.push(val);
  }

  withraw(val) {
    this.deposit(-val);
  }

  _approveLoan(val) {
    return true;
  }

  requestLoan(val) {
    if (this._approveLoan(val)) {
      this.deposit(val);
      console.log('恭喜你!贷款成功');
    }
  }
}

私有字段

● 例如存款取款记录是私有字段,我们不希望可以被直接访问,从 ECMAScript 2022 开始,JavaScript 引入了私有字段的语法,例如 #privateField。

javascript 复制代码
class Account {
  //公有字段(例子,实际与之前写法作用一样)
  locale = navigator.language;

  //私有字段
  #movements = [];
  #pin;

  constructor(owner, currency, pin) {
    //参数中不可以直接为私有字段
    this.owner = owner;
    this.currency = currency;
    this.#pin = pin;

    console.log(`欢迎来到你的账户,${owner}`);
  }

  //公共接口
  getMovements() {
    return this.#movements;
  }

  deposit(val) {
    this.#movements.push(val);
  }

  withraw(val) {
    this.deposit(-val);
  }

  _approveLoan(val) {
    return true;
  }

  requestLoan(val) {
    if (this._approveLoan(val)) {
      this.deposit(val);
      console.log('恭喜你!贷款成功');
    }
  }
}

● 这样我们无法直接查看这个字段,当然使用getMovements同样可以访问,因为她还是一个公有的API

javascript 复制代码
console.log(ITshare.#movements);

公共方法

这个就不在赘述在了,在上述的构造器中,所有的方法都是公共方法

私有方法

私有方法和私有字段的表现方式一样

javascript 复制代码
class Account {
  //公有字段(例子,实际与之前写法作用一样)
  locale = navigator.language;

  //私有字段
  #movements = [];
  #pin;

  constructor(owner, currency, pin) {
    //参数中不可以直接为私有字段
    this.owner = owner;
    this.currency = currency;
    this.#pin = pin;

    console.log(`欢迎来到你的账户,${owner}`);
  }

  //公共接口
  getMovements() {
    return this.#movements;
  }

  deposit(val) {
    this.#movements.push(val);
  }

  withraw(val) {
    this.deposit(-val);
  }

  requestLoan(val) {
    if (this.#approveLoan(val)) {
      this.deposit(val);
      console.log('恭喜你!贷款成功');
    }
  }

  //私有方法
  #approveLoan(val) {
    return true;
  }
}
相关推荐
喵叔哟7 小时前
9. 【Blazor全栈开发实战指南】--Blazor调用JavaScript
开发语言·javascript·udp
我命由我123457 小时前
Element Plus - Form 的 resetField 方法观察记录
开发语言·前端·javascript·vue.js·html·html5·js
清空mega8 小时前
《Vue3 项目结构详解:components、views、assets、router、stores 到底该怎么理解?》
前端·javascript·vue.js
雨雨雨雨雨别下啦9 小时前
Vue——小白也能学!Day6
前端·javascript·vue.js
冰暮流星9 小时前
javascript如何实现删除数组里面的重复元素
开发语言·前端·javascript
网络点点滴11 小时前
透传属性$attrs
前端·javascript·vue.js
Moment11 小时前
尤雨溪宣布 Vite+ 正式开源,前端工具链要大一统了
前端·javascript·面试
喵叔哟11 小时前
5. 【Blazor全栈开发实战指南】--Blazor组件基础
开发语言·javascript·ecmascript
ujainu12 小时前
Electron 实战:将用户输入保存到本地文件 —— 基于 `fs.writeFileSync` 与 IPC 的安全写入方案
javascript·安全·electron
进击的尘埃13 小时前
基于 LangChain.js 的前端 Agent 工作流编排:Tool 注册、思维链可视化与多步推理的实时 DAG 渲染
javascript