JS的get和set使用示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JS的get和set使用示例

  2020-05-13 我要评论
巧用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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们