当程序查询对象属性时调用get方法,如果只有get方法那么他是一个只读属性,//程序对对象属性进行赋值操作时调用set方法,如果只有set方法那么他是是一个只读属性
<script type="text/javascript">
var p = {
x:1.0,
y:1.0,
//当程序查询对象属性时调用get方法,如果只有该属性标识这是一个只读属性
// get r(){
// console.log('this.x->',this.x,'this.y->',this.y)
// return Math.sqrt(this.x*this.x+this.y*this.y)
// },
//程序对对象属性进行赋值操作时调用set方法,如果只有该属性标识这是一个只写属性
set r(newvalue){
var oldvalue = Math.sqrt(this.x*this.x+this.y*this.y)
var ratio = newvalue/oldvalue
this.x *= ratio
this.y *= ratio
}
}
// var q = inherit(p)
// q.x = 1,q.y = 1;
console.log('1->',p.r)//注释掉get r 这里就是undefine
console.log(p.x,p.y)
p.r = 4//注释掉set r 这个赋值操作无效
console.log('2->',p.r)//注释掉get r 这里就是undefine
console.log(p.x,p.y)
</script>