加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

JS的get和set使用范例

发布时间:2023-10-18 15:21:17 所属栏目:教程 来源:
导读:巧用get和set,能够直接操作对象属性实现读写,可以极大的提高编程效率,给出一个典型示例:

代码如下:

var test = {

_Name : null,

_Age : 0,

//_Name的读写

set name(name) {this._Name =
巧用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
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章