JS get and set use examples
- 2020-03-30 02:05:37
- OfStack
Clever use of get and set, can directly manipulate object properties to achieve read and write, can greatly improve the programming efficiency, give a typical example:
var test = {
_Name : null,
_Age : 0,
//_Name read and write
set name(name) {this._Name = name;},
get name() {return this._Name;},
//_Age read and write
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