龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

JS的get和set使用示例

时间:2014-06-16 02:11来源:网络整理 作者:网络 点击:
分享到:
巧用get和set,能够直接操作对象属性实现读写,可以极大的提高编程效率,下面有个不错的示例,大家可以参考下
巧用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
精彩图集

赞助商链接