<!DOCTYPE html>
<html>
<head>
<mata charset="UTF-8" />
<title>回顾Object.defineproperty方法</title>
</head>
<body>
<script type="text/javascript">
let number = 20
let person = {
name:'张三',
sex:'男',
}
Object.defineProperty(person,'age',{
//value:18,
//enumerable:true,//控制属性是否可以枚举,默认false
//writable:true,//控制属性是否可以被修改,默认false
//configurable:true,//控制属性是否可以被删除,默认false
//当有人读取person的age属性时,get函数(getter)就会被调用,且返回值就是age的值
get:function(){
console.log('有人读取age属性了')
return number
},
set(value){
console.log('有人修改了age属性,且值是',value)
number = value
}
})
// console.log(Object.keys(person))
console.log(person)
</script>
</body>
</html>