html5中文学习网

您的位置: 首页 > 网站及特效实例 > javascript特效 » 正文

JS的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
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助