当前位置:网站首页>Object.defineProperty实时监听数据变化并更新页面
Object.defineProperty实时监听数据变化并更新页面
2022-08-05 01:03:00 【追逐梦想之路_随笔】
//html
<ul></ul>
//js部分
<script>
var ul = document.querySelector('ul');
var person = { sex: '男', age: '25', name: '王大锤', height: 28, weight: 32 };
const _render = element => {
var str = `<li>姓名:<span>${person.name}</span></li>
<li>性别:<span>${person.sex}</span></li>
<li>年龄:<span>${person.age}</span></li>
<li>身高:<span>${person.height}</span></li>
<li>体重:<span>${person.weight}</span></li>`
element.innerHTML = str;
}
_render(ul);
// 补全代码
// 监听对象入口
function obseve(targetObj){
if(targetObj === null || typeof targetObj !== "object" ){
return targetObj
}
for (const key in targetObj) {
defineReactive(targetObj,key,targetObj[key])
}
}
// 正式劫持和监听对象
function defineReactive(targetObj,key,value){
Object.defineProperty(targetObj,key,{
get(){
return value
},
set(setVal){
if(setVal !== value){
value = setVal
_render(ul)
}
}
})
}
// 传入需要监听的对象
obseve(person)
边栏推荐
猜你喜欢
4. PCIe interface timing
深度学习原理学习小结 - Self-Attention/Transformer
Binary tree [full solution] (C language)
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
Use of pytorch: Convolutional Neural Network Module
Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
习题:选择结构(一)
day14--postman interface test
Matlab uses plotting method for data simulation and simulation
随机推荐
Software testing interview questions: What are the seven-layer network protocols?
GCC:编译时库路径和运行时库路径
快速批量修改VOC格式数据集标签的文件名,即快速批量修改.xml文件名
蓝牙Mesh系统开发四 ble mesh网关节点管理
Lattice PCIe Learning 1
Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
Activity Recommendation | Kuaishou StreamLake Brand Launch Conference, witness together on August 10!
LiveVideoStackCon 2022 上海站明日开幕!
gorm joint table query - actual combat
Difference between MBps and Mbps
软件基础的理论
Why is this problem reported when installing oracle11
Software testing interview questions: test life cycle, the test process is divided into several stages, and the meaning of each stage and the method used?
【TA-霜狼_may-《百人计划》】图形4.3 实时阴影介绍
GCC: compile-time library path and runtime library path
canvas Gaussian blur effect
How DHCP works
GCC: Shield dependencies between dynamic libraries
Introduction to JVM class loading
GCC: paths to header and library files