html_and_vue/vue/Object.defineproperty.html

40 lines
1.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
let number = 18
let person = {
name:'Cori',
sex:'male',
// age:18
}
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
},
// 当有人修改person的age属性时set函数setter就会被调用且会收到修改的具体值
set(value){
console.log('有人修改age属性了,且值是', value)
number = value
}
})
console.log(person)
</script>
</body>
</html>