当前位置: 首页 > news >正文

class的访问器成员

class的访问器成员

  • 本质是 class 的语法糖 等价于对象的defineProperty
  • 对象里面也能使用
class Product{constructor(count, price){this.count = count;this.price = price;}get total(){ // 相当于getterreturn this.count * this.price;}}const product = new Product(10, 100);console.log(product.total); // 1000product.count = 20;console.log(product.total); // 2000
const product = {price: 100,count: 10,get total() { // 相当于getterreturn this.price * this.count;},set total(value) { // 相当于setter 改变时会影响price的值.this.price = value / this.count; },};
console.log(product.total); // 1000
product.count = 20;
console.log(product.total); // 2000
http://www.dtcms.com/a/138983.html

相关文章:

  • TAS(Thin-Agent服务)的先决条件与安装指南
  • 安当ASP身份认证系统:低成本方案实现堡垒机/防火墙/VPN二次认证升级
  • 《Learning Langchain》阅读笔记2-基于 Gemini 的 Langchain PromptTemplate 实现方式
  • [C++] STL中的向量容器<vector>附加练习
  • 赛灵思 XCVU440-2FLGA2892E XilinxFPGA Virtex UltraScale
  • Qt 信号与槽复习
  • 【Springboot】项目Demo
  • git rebase的使用
  • 某客户ORA-600 导致数据库反复重启问题分析
  • 如何判断单片机性能极限?
  • Linux 网络配置
  • OpenHarmony - 小型系统内核(LiteOS-A)(七)
  • GPT,Bert类模型对比
  • 16-算法打卡-哈希表-两个数组的交集-leetcode(349)-第十六天
  • 使用 PM2 启动node服务,并添加监控接口
  • Linux系统之restore命令的基本使用
  • d3.js绘制组合PCA边缘分布图
  • 数据结构(6)
  • MYOJ_11700(UVA10591)Happy Number(快乐数)(超快解法:图论思想解题)
  • 阿尔特拉 EP1C12F324I7N AlteraFPGA Cyclone
  • Redis——数据结构
  • 【ELF2学习板】OpenCL程序测试
  • 逻辑删除表结构如何加唯一索引?
  • Obsidian的简单使用
  • 【Semantic Kernel核心组件】Kernel:掌控AI编排的“中央处理器“
  • Java基础知识面试题(已整理Java面试宝典pdf版)
  • AbMole—如何高效诱导巨噬细胞的极化?
  • 04-libVLC的视频播放器:获取媒体信息
  • 《手环表带保养全攻略:材质、清洁与化学品避坑指南》
  • 力扣349 == 两个数组交集的两种解法