当前位置:网站首页>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)
边栏推荐
- ora-00604 ora-02429
- 2022 Nioke Multi-School Training Session H Question H Take the Elevator
- Software testing interview questions: How many types of software are there?
- How DHCP works
- 软件基础的理论
- C# const readonly static 关键字区别
- Software Testing Interview Questions: What Are the Types of Software Testing?
- Software testing interview questions: What are the strategies for system testing?
- 创意代码表白
- Software Testing Interview Questions: About Automated Testing Tools?
猜你喜欢
安装oracle11的时候为什么会报这个问题
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
【Redis】Linux下Redis安装
5. PCIe official example
If capturable=False, state_steps should not be CUDA tensors
【TA-霜狼_may-《百人计划》】图形4.3 实时阴影介绍
ORA-01105 ORA-03175
新来个技术总监,把DDD落地的那叫一个高级,服气
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
随机推荐
oracle create user
(十七)51单片机——AD/DA转换
PCIe Core Configuration
[GYCTF2020]EasyThinking
新来个技术总监,把DDD落地的那叫一个高级,服气
详细全面的postman接口测试实战教程
Software Testing Interview Questions: What's the Key to a Good Test Plan?
DHCP的工作过程
Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?
5. PCIe official example
ora-00604 ora-02429
金九银十面试跳槽季;你准备好了吗?
Lattice PCIe 学习 1
快速批量修改VOC格式数据集标签的文件名,即快速批量修改.xml文件名
Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?
行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
day14--postman interface test
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
习题:选择结构(一)
【TA-霜狼_may-《百人计划》】图形4.3 实时阴影介绍