当前位置:网站首页>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)
边栏推荐
猜你喜欢

Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?

安装oracle11的时候为什么会报这个问题
![Binary tree [full solution] (C language)](/img/4d/2d81dc75433c23c5ba6b31453396f0.png)
Binary tree [full solution] (C language)

"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology

多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)

Lattice PCIe Learning 1

执掌图表

从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】

JUC thread pool (1): FutureTask use

Matlab uses plotting method for data simulation and simulation
随机推荐
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
Software testing interview questions: What stages should a complete set of tests consist of?
tensor.nozero(),面具,面具
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
pytorch的使用:卷积神经网络模块
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
GCC: compile-time library path and runtime library path
The method of freely controlling concurrency in the sync package in GO
C# const readonly static 关键字区别
Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
新来个技术总监,把DDD落地的那叫一个高级,服气
Software testing interview questions: Please draw the seven-layer network structure diagram of OSI and the four-layer structure diagram of TCP/IP?
PCIe 核配置
Bit rate vs. resolution, which one is more important?
【七夕如何根据情侣倾听的音乐进行薅羊毛】背景音乐是否会影响情侣对酒的选择
(十七)51单片机——AD/DA转换
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac