JS的get和set使用示例
更新时间:2014年02月20日 09:31:40 作者:
巧用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
复制代码 代码如下:
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
相关文章
全面了解JavaScirpt 的垃圾(garbage collection)回收机制
下面小编就为大家带来一篇全面了解JavaScirpt 的垃圾(garbage collection)回收机制。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-07-07想学习javascript JS和jQuery哪个重要 先学哪个
在一些技术论坛与qq群经常看到有这样类似的提问,当然提出这样问题的通常都是新手为了解决大家的疑惑,同时帮助新手程序员能更快掌握学习的方向,不致于弄错重点2016-12-12分析Node.js connect ECONNREFUSED错误
最近在准备Angularjs +node.js demo的时候在我的mac开发中 遇见此错误2013-04-04
最新评论