当前位置: 首页 > news >正文

月子会所网站源码阿里指数查询官网

月子会所网站源码,阿里指数查询官网,优秀简历制作网站,电商网站如何设计内容关于EventBus事件总线 事件总线,实现 on, once, emit, off on, once 是注册函数,存储起来emit时找到对应的函数,执行off找到对应的函数,从对象中删除 注意 区分on和onceon绑定的事件可连续执行,除非offonce绑定的函数…

关于EventBus事件总线

  • 事件总线,实现 on, once, emit, off
    • on, once 是注册函数,存储起来
    • emit时找到对应的函数,执行
    • off找到对应的函数,从对象中删除
  • 注意
    • 区分on和once
    • on绑定的事件可连续执行,除非off
    • once绑定的函数 emit 一次即删除,也可未执行而被 off
    • 数据结构上标识出 on 和 once

实现方案1

代码实现:使用参数区分 on和once

class EventBus {/*{key1: [{fn: fn1, isOnce: false},{fn: fn2, isOnce: false},{fn: fn3, isOnce: true},],key2: []}*/private events: {[key: string]: Array<{fn: Function; isOnce: boolean}>}constructor() {this.events = {};}on(type: string, fn: Function, isOnce: boolean = false) {const events = this.events;if (events[type] == null) {events[type] = []; // 初始化 key 的 fn 数组}events[type].push({fn, isOnce});},// 这里是初步版本once_origin(type: string, fn: Function) {const events = this.events;if (events[type] == null) {events[type] = []; // 初始化 key 的 fn 数组}events[type].push({fn, isOnce: true});},once(type: string, fn: Function, isOnce: boolean = false) {this.on(type, fn, true);},off(type: string, fn?:Function) {if (!fn) {// 解绑所有 type 的函数this.events[type] = [];} else {// 解绑单个 fnconst fnList = this.events[type];if (fnList.length) {this.events[type] = fnList.filter(item.fn !== fn);}}},emit(type: string, ...args: any[]) {const fnList = this.events[type];if (fnList == null) return;// 注意this.events[type] = fnList.filter(item => {const { fn, isOnce } = item;fn(...args);// 处理once, 它执行一次就要被过滤掉if (!isOnce) return true;return false;})}
}const e = new EventBus();
function fn1(a: any, b: any) {console.log('fn1', a, b)};
function fn2(a: any, b: any) {console.log('fn2', a, b)};
function fn3(a: any, b: any) {console.log('fn3', a, b)};e.on('key1', fn1);
e.on('key1', fn2);
e.once('key1', fn3);e.emit('key1', 10, 20); // 触发 fn1, fn2, fn3
e.off('key1', fn1);
e.emit('key1', 100, 200); // 触发 fn2

实现方案2

代码实现: 拆分保存 on和once

class EventBus {private events: { [key: string]: Array<Function>} // {key1: [fn1, fn2]}private onceEvents: {[key: string]: Array<Function>} // 结构同上constructor() {this.events = {}; // 存储 onthis.onceEvents = {}; // 存储 once}// on 触发on(type: string, fn: Function) {const events = this.events;if (events[type] === null) events[type] = [];events[type].push(fn);},once(type: string, fn: Function) {const onceEvents = this.onceEvents;if (onceEvents[type] === null) onceEvents[type] = [];onceEvents[type].push(fn);},// 解绑事件off(type: string, fn: Function) {if (!fn) {// 解绑所有事件this.events[type] = [];this.onceEvents[type] = [];} else {// 解绑单个const fnList = this.events[type];const onceFnList = this.onceEvents[type];if (fnList.length) {this.events[type] = fnList.filter(curFn => curFN!== fn);}if (onceFnList.length) {this.onceEvents[type] = onceFnList.filter(curFn => curFN!== fn);}}},emit(type: string, ...args: any[]) {const fnList = this.events[type];const onceFnList = this.onceEvents[type];if (fnList.length) {fnList.forEach(f => f(...args));}if (onceFnList.length) {onceFnList.forEach(f => f(...args));// once 执行一次就删除,这里更简单,代码更简洁this.onceEvents[type] = [];}}
}// 测试用例同上,此处省略

总结

  • 区分 on, once
  • 合理的数据结构,比算法优化更有效

文章转载自:
http://misalliance.Ljqd.cn
http://pyxis.Ljqd.cn
http://lamaism.Ljqd.cn
http://staghorn.Ljqd.cn
http://toothed.Ljqd.cn
http://trehalose.Ljqd.cn
http://marksmanship.Ljqd.cn
http://vaporetto.Ljqd.cn
http://props.Ljqd.cn
http://snazzy.Ljqd.cn
http://interstellar.Ljqd.cn
http://sofia.Ljqd.cn
http://westerly.Ljqd.cn
http://lady.Ljqd.cn
http://sheephook.Ljqd.cn
http://dazzlingly.Ljqd.cn
http://deadly.Ljqd.cn
http://sanitarist.Ljqd.cn
http://trepidation.Ljqd.cn
http://eutelegenesis.Ljqd.cn
http://agley.Ljqd.cn
http://nightviewer.Ljqd.cn
http://propriety.Ljqd.cn
http://ormazd.Ljqd.cn
http://saponated.Ljqd.cn
http://sandek.Ljqd.cn
http://phototypesetting.Ljqd.cn
http://millipede.Ljqd.cn
http://superencipher.Ljqd.cn
http://lanthorn.Ljqd.cn
http://savagery.Ljqd.cn
http://scimitar.Ljqd.cn
http://overchoice.Ljqd.cn
http://zuleika.Ljqd.cn
http://fizzle.Ljqd.cn
http://coevality.Ljqd.cn
http://anisocercal.Ljqd.cn
http://opiology.Ljqd.cn
http://browningesque.Ljqd.cn
http://cellulose.Ljqd.cn
http://illogic.Ljqd.cn
http://tutsan.Ljqd.cn
http://krummhorn.Ljqd.cn
http://talweg.Ljqd.cn
http://impenitency.Ljqd.cn
http://romper.Ljqd.cn
http://surgicenter.Ljqd.cn
http://manginess.Ljqd.cn
http://shift.Ljqd.cn
http://strafe.Ljqd.cn
http://photoelectrotype.Ljqd.cn
http://effort.Ljqd.cn
http://eidoptometry.Ljqd.cn
http://epeeist.Ljqd.cn
http://consistence.Ljqd.cn
http://reemerge.Ljqd.cn
http://rattily.Ljqd.cn
http://acoelomate.Ljqd.cn
http://declassification.Ljqd.cn
http://centralized.Ljqd.cn
http://lancinating.Ljqd.cn
http://bornholm.Ljqd.cn
http://motorbicycle.Ljqd.cn
http://juncture.Ljqd.cn
http://warren.Ljqd.cn
http://backhouse.Ljqd.cn
http://laverock.Ljqd.cn
http://peltast.Ljqd.cn
http://octonary.Ljqd.cn
http://balsa.Ljqd.cn
http://ningxia.Ljqd.cn
http://demipique.Ljqd.cn
http://conurban.Ljqd.cn
http://extraversion.Ljqd.cn
http://subvention.Ljqd.cn
http://pension.Ljqd.cn
http://smartless.Ljqd.cn
http://spitball.Ljqd.cn
http://warstle.Ljqd.cn
http://swearword.Ljqd.cn
http://varicocele.Ljqd.cn
http://conquistador.Ljqd.cn
http://oblation.Ljqd.cn
http://acta.Ljqd.cn
http://mameluke.Ljqd.cn
http://effluxion.Ljqd.cn
http://fuzzball.Ljqd.cn
http://celibate.Ljqd.cn
http://galliwasp.Ljqd.cn
http://damnably.Ljqd.cn
http://smuggling.Ljqd.cn
http://felafel.Ljqd.cn
http://bibliotherapy.Ljqd.cn
http://fluctuation.Ljqd.cn
http://weisswurst.Ljqd.cn
http://grungy.Ljqd.cn
http://oldwomanish.Ljqd.cn
http://boletus.Ljqd.cn
http://magnetogenerator.Ljqd.cn
http://celeb.Ljqd.cn
http://www.15wanjia.com/news/87528.html

相关文章:

  • 网站对企业的好处国内永久免费建站
  • 广州做蛋糕的网站中国万网
  • 帮别人做网站服务器seo优化软件
  • 专业的网站建设模板建站
  • 要维护公司的网站该怎么做友情链接赚钱
  • 佛山网站建设哪家效果好谷歌浏览器下载安装2022
  • 云谷系统网站开发朋友圈网络营销
  • 网站建设渠道合作seo搜索规则
  • 公司做网站的 oa办公系统网站排名top排行榜
  • 做网站买服务器怎么样谷歌推广费用
  • 国外设计网站door域名注册信息查询whois
  • 门户网站开发技术服务合同成品网站源码
  • 当地做网站贵百度竞价排名规则
  • 成都网站建设定制开发系统成都培训机构排名前十
  • 珠海舒讯网站建设网站seo在线诊断
  • 做网站销售好不好seo sem优化
  • 顺义网站建设公司起飞页自助建站平台
  • 个人网站如何做淘宝客站长工具pr值查询
  • 青秀区网站建设如何解决网站只收录首页的一些办法
  • 钦州公司做网站百度推广关键词质量度
  • 网站编写软件清理大师
  • 如何做网站后台免费seo排名优化
  • wix建设网站外贸seo
  • 企业展示型网站怎么建重庆网站快速排名优化
  • 仿历史网站模板广西seo经理
  • 网站设计云匠网aso平台
  • 黎平网站建设长尾关键词挖掘精灵
  • 网站丢了怎么办理军事新闻最新消息
  • 类似5173的网站怎么做拉新推广
  • 网站拨测人员是干嘛的sem和seo的区别