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

相关文章:

  • 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
  • 白天气温超30℃的北京,晚间下起了冰雹
  • 日本广岛大学一处拆迁工地发现疑似未爆弹
  • 吉林:消纳绿电,“氢”装上阵
  • 夜读丨取稿费的乐趣
  • 2024年度全国秋粮收购达3.45亿吨
  • 巴西总统卢拉昨晚抵达北京