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

关爱一线防疫工作者,浩城嘉业携手高米店街道办事处共筑公益长城

TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format

You can change this value on the server by setting the 'Max_ allowed_ Packet 'variable error

Trust multithread security count

There was an error while marking a file for deletion

Tang's little helper

1---电子实物认知

Advanced software testing - test classification

1--- electronic physical cognition

乐观锁解析
随机推荐
ORB_ Slam3 recurrence - Part I
乐观锁解析
JZ32 从上往下打印二叉树
如何将exe文件添加到开机启动
JVM基础和问题分析入门笔记
推特收购舆论战,被马斯克变成了小孩吵架
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
JZ71 跳台阶扩展问题
年轻时代,噢,年轻时代
[noi2015] package manager
Diagonalization, power of a
Chapter 5 Basic Scripting: Shell Variables
C盘空间不够 mklink解决VScode扩展迁移到其他盘
One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress
Introduction notes of JVM foundation and problem analysis
Project: serial port receiving RAM storage TFT display (complete design)
GAN的详细介绍及其应用(全面且完整)
NC15 求二叉树的层序遍历
MySQL index optimization introduction
Common file operations