- get 无参函数,在属性被读取时工作
- set 有一个参数的函数,在属性被设置时工作
- enumerable 可否列举
- configurable 可否配置
1 let user = { 2 name: "John", 3 surname: "Smith" 4 }; 5 6 Object.defineProperty(user, 'fullName', { 7 get() { 8 return `${this.name} ${this.surname}`; 9 }, 10 11 set(value) { 12 [this.name, this.surname] = value.split(" "); 13 } 14 }); 15 16 alert(user.fullName); // John Smith 17 18 for(let key in user) alert(key); // name, surname