巧用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
最后
以上就是冷酷橘子最近收集整理的关于JS的get和set使用示例的全部内容,更多相关JS内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复