<!DOCTYPE html>
<html>
<head>
<title>document.write() Example</title>
</head>
<body>
<script type="text/javascript">
let car = {}
let val = 3000
Object.defineProperty(car, 'price', {
enumerable: true,
configurable: true,
get(){
console.log('price属性被读取了')
return val
},
set(newVal){
console.log('price属性被修改了')
val = newVal
}
})
</script>
</body>
</html>
car.price
12:13:47.223 43.html:14 price属性被读取了
12:14:32.210 3000
12:14:32.214 3000
12:14:49.743 car.price = 3000
12:14:49.744 43.html:18 price属性被修改了
12:14:49.747 3000
12:15:02.690 car.price = 5000
12:15:02.690 43.html:18 price属性被修改了
12:15:02.693 5000
https://vue-js.com/learn-vue/reactive/object.html#_2-%E4%BD%BFobject%E6%95%B0%E6%8D%AE%E5%8F%98%E5%BE%97-%E5%8F%AF%E8%A7%82%E6%B5%8B