JS的get和set使用示例

时间:2024-05-13 09:35:45 

巧用get和set,能够直接操作对象属性实现读写,可以极大的提高编程效率,给出一个典型示例:


var test = {
_Name : null,
_Age : 0,

//_Name的读写
set name(name) {this._Name = name;},
get name() {return this._Name;},
//_Age的读写
set age(age) {this._Age = age;},
get age() {return this._Age;}
}

alert(test.name + " " + test.age);//bull 0
test.name = 'lucy';
test.age = 20;
alert(test.name + " " + test.age);//lucy 20
标签:get,set
0
投稿

猜你喜欢

  • python人工智能tensorflow常用激活函数Activation Functions

    2023-08-01 12:45:02
  • python实现二叉树的遍历

    2023-06-29 17:45:42
  • BeautifulSoup中find和find_all的使用详解

    2023-11-08 21:00:22
  • 分析详解python多线程与多进程区别

    2022-01-19 01:13:54
  • Python时间差中seconds和total_seconds的区别详解

    2022-10-04 15:00:49
  • FrontPage XP设计教程2——网页的编辑

    2008-10-11 12:16:00
  • SQL语句分组获取记录的第一条数据的方法

    2012-08-21 10:58:39
  • MySQL数据库开发的36条原则(小结)

    2024-01-23 23:34:48
  • [原创][分享]数字格式化转换

    2011-07-04 12:20:15
  • MySQL DQL语句的具体使用

    2024-01-21 08:17:03
  • Python数据类型详解(一)字符串

    2023-08-12 22:55:56
  • python调用Moxa PCOMM Lite通过串口Ymodem协议实现发送文件

    2023-09-08 09:30:22
  • MySQL 字符串模式匹配 扩展正则表达式模式匹配

    2024-01-20 14:47:41
  • Vscode的SSH插件远程连接Linux的实现步骤

    2023-09-07 17:57:11
  • tensorflow之读取jpg图像长和宽实例

    2023-10-12 11:57:41
  • 原生js实现autocomplete插件

    2024-04-17 09:44:55
  • 在Python中使用第三方模块的教程

    2022-09-06 12:20:27
  • MySql 5.6.35 winx64 安装详细教程

    2024-01-24 13:36:13
  • python import 上级目录的导入

    2021-09-13 00:54:29
  • python 解决微分方程的操作(数值解法)

    2021-08-11 23:50:24
  • asp之家 网络编程 m.aspxhome.com