console
const $app = document.querySelector('#app')
const bucket = new Set()
const state = new Proxy({ text: 'hello akimixu' }, {
get(target, key) {
const value = target[key]
bucket.add(effect)
console.log(`get $ {key}: $ {value}`)
return value
},
set(target, key, newValue) {
console.log(`set $ {key}: $ {newValue}`)
target[key] = newValue
bucket.forEach((fn) => fn())
}
})
function effect() {
console.log("执行了对应的fn")
$app.innerText = state.text
}
effect()
setTimeout(() => {
state.text = 'hello xuqiushi'
}, 2000)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
#app{
width: 200px;
height: 100px;
background-color: #ffffff;
}