当前位置:网站首页>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)
边栏推荐
- EL定时刷新页面中的皕杰报表实例
- 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?
- 蓝牙Mesh系统开发五 ble mesh设备增加与移除
- Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
- Opencv——视频跳帧处理
- GCC:编译时库路径和运行时库路径
- GCC: paths to header and library files
- Lattice PCIe Learning 1
- Lattice PCIe 学习 1
- Opencv - video frame skipping processing
猜你喜欢

(十七)51单片机——AD/DA转换

oracle create tablespace

Bit rate vs. resolution, which one is more important?

Why is this problem reported when installing oracle11

Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!

Day Fourteen & Postman
![[GYCTF2020]EasyThinking](/img/40/973411c69d1e4766d22f6a4a7c7c01.png)
[GYCTF2020]EasyThinking

面试汇总:为何大厂面试官总问 Framework 的底层原理?
![[Machine Learning] 21-day Challenge Study Notes (2)](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Machine Learning] 21-day Challenge Study Notes (2)

缺陷检测(图像处理部分)
随机推荐
Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
ORA-00604 ORA-02429
多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
[GYCTF2020]EasyThinking
JVM类加载简介
4. PCIe interface timing
JUC thread pool (1): FutureTask use
第十一章 开关级建模
手把手基于YOLOv5定制实现FacePose之《YOLO结构解读、YOLO数据格式转换、YOLO过程修改》
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?
(十七)51单片机——AD/DA转换
Opencv——视频跳帧处理
FSAWS 的全球基础设施和网络
PCIe 核配置
PCIe Core Configuration
MBps与Mbps区别
2021年11月网络规划设计师上午题知识点(下)
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?
2022 Nioke Multi-School Training Session H Question H Take the Elevator