Using get and set skillfully can directly manipulate object properties to realize read and write, which can greatly improve programming efficiency. A typical example is given:
The code copy is as follows:
var test = {
_Name : null,
_Age : 0,
// Read and write of_Name
set name(name) {this._Name = name;},
get name() {return this._Name;},
// Read and write of_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