javascript get set读取器
1 class Person{ 2 3 constructor(name, id){ 4 5 let _name = name, _id = id; 6 7 Object.defineProperties(this, { 8 9 name:{ 10 //每次获取this.name的时候都会调用get方法 11 get:()=>{return _name;}, 12 //每次修改this.name的时候都会调用set方法 13 set:(v)=>{_name = v; document.write(v);} 14 }, 15 16 id:{ 17 get:()=>{return _id;}, 18 set:(v)=>{_id = v; document.write(v);} 19 } 20 21 }); 22 23 } 24 25 } 26 //test 27 let p = new Person("w_1"); 28 console.log(p); 29 p.name = "w_2";//w_2