對象的存儲描述: get 和 set 方法 1 <script> 2 var obj ={ 3 get age(){ 4 return 22 5 }, 6 set age(value){ 7 console.log(value) 8 } 9 } 10 console.log(obj.age) 11
對象的存儲描述:
get 和 set 方法
1 <script> 2 var obj ={ 3 get age(){ 4 return 22 5 }, 6 set age(value){ 7 console.log(value) 8 } 9 } 10 console.log(obj.age) 11 obj.age = 23 12 13 var obj1 ={ 14 x:1, 15 y:2, 16 z:3, 17 get zhouchang(){ 18 return this.x+this.y+this.z 19 }, 20 set two(value){ 21 this.x*=value 22 this.y*=value 23 this.z*=value 24 } 25 } 26 console.log(obj1.zhouchang) 27 obj1.two =2 28 console.log(obj1.zhouchang) 29 30 Object.defineProperty(obj1,"k",{ 31 get : function(){ 32 return 29 33 } 34 }) 35 console.log(obj1.k) 36 </script>