当前位置:网站首页>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)
边栏推荐
- 【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
- torch.autograd.grad finds the second derivative
- 工具类总结
- Software Testing Interview Questions: What's the Key to a Good Test Plan?
- Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
- ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
- [GYCTF2020]EasyThinking
- CNI (Container Network Plugin)
- oracle create user
- Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
猜你喜欢
CNI(Container Network Plugin)
oracle create user
[Machine Learning] 21-day Challenge Study Notes (2)
Binary tree [full solution] (C language)
JVM类加载简介
Day Fourteen & Postman
Matlab uses plotting method for data simulation and simulation
深度学习原理学习小结 - Self-Attention/Transformer
Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
Inter-process communication and inter-thread communication
随机推荐
oracle create user
5.PCIe官方示例
【机器学习】21天挑战赛学习笔记(二)
The principle of NMS and its code realization
Pytorch使用和技巧
Methods commonly used interface automation test framework postman tests
蓝牙Mesh系统开发五 ble mesh设备增加与移除
PCIe 核配置
新唐NUC980使用记录:在用户应用中使用GPIO
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
CNI(Container Network Plugin)
MongoDB搭建及基础操作
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
GCC: compile-time library path and runtime library path
测试工作这么难找吗?今年32,失业2个月,大龄测试工程师接下来该拿什么养家?
[Machine Learning] 21-day Challenge Study Notes (2)
方法重写与Object类
安装oracle11的时候为什么会报这个问题
2021年11月网络规划设计师上午题知识点(下)
Is DDOS attack really unsolvable?Do not!