当前位置:网站首页>ES6通过代理器(Proxy)与反射(Reflect)实现观察者模式
ES6通过代理器(Proxy)与反射(Reflect)实现观察者模式
2022-07-25 18:38:00 【RemoteDev】
//观察者模式
let queueObserve = new Set();//观察池
let observe = fn=>queueObserve.add(fn);//观察者
let observable=data=>new Proxy(data,{set});//数据触发者,通过代理器实现
function set(t,k,v,r) {
let d = Reflect.set(t,k,v,r);
// @ts-ignore
queueObserve.forEach(v=>v());//遍历池并调用池中方法
return d;
}
//观察数据对象
let data=observable({
name:'RemoteDev',
job:'IT'
});
function print() {
console.log(`${data.name},${data.job}`);
}
observe(print);//观察者
data.name='RM';//数据发生变化,调用observable--->set--->遍历池边栏推荐
- Common file operations
- 3de 回复
- 拍卖行作VC,第一次出手就投了个Web3
- Safe operation instructions for oscilloscope probe that must be read by engineers
- 用GaussDB(for Redis)存画像,推荐业务轻松降本60%
- Ceres curve fitting
- 超全Mavan标签详解
- One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress
- ZFS - 01 - basic operations of creating and expanding zpool
- Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
猜你喜欢

3DE reply

If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing

MySQL index optimization introduction

Trust multithread security count

年轻时代,噢,年轻时代

n-queens problem

Safe operation instructions for oscilloscope probe that must be read by engineers

如何将exe文件添加到开机启动

Powell's function of Ceres

Advanced software testing - test classification
随机推荐
【翻译】Logstash、Fluentd、Fluent Bit,还是Vector?如何选择合适的开源日志收集器...
一次备库的坏块的修复过程
Partial correlation calculation of R language and partial correlations calculation using pcor function of GGM package
大厂云业务调整,新一轮战争转向
Tang's little helper
市值300亿,欧洲十年来最大IPO再冲纽交所
Chapter 5 Basic Scripting: Shell Variables
7. 依赖注入
win11下vscode 自动升级失败 There was an error while marking a file for deletion
[HAOI2015]树上操作
用GaussDB(for Redis)存画像,推荐业务轻松降本60%
Circulaindicator component, which makes the indicator style more diversified
uniapp滚动条置顶效果、自定义页面滚动条的位置(整理)
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
R language uses GT package and gtextras package to display tabular data beautifully: GT_ bar_ Plot function and GT_ plt_ bar_ PCT function visual percentage bar graph, percentage bar graph of original
Project: serial port receiving RAM storage TFT display (complete design)
Optimistic lock resolution
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
What is the difference between GB and gib disk space units?
一周活动速递|深入浅出第8期;Meetup成都站报名进行中