当前位置: 首页 > 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://www.yayakq.cn/news/799394/

相关文章:

  • 建网站行业给期货交易类做网站违法吗
  • 网站开发前端基础天津百度seo排名优化
  • 网站老域名跳转到新域名平台网站开发的税率
  • 网站制作哪家好网站风格什么意思
  • 我要表白网站在线制作网络销售的工作内容
  • 网站建设必会的软件有哪些无锡专业网站营销
  • 如何在百度发布广告信息如何seo推广
  • 专门发布采购信息的网站福州市网站建设公司
  • 用php做电子商务网站软件项目管理第二版课后答案
  • 网站模板怎么替换微信小程序推广方法
  • 什么网站百度收录快wordpress推荐插件
  • 有哪些出名的工业设计网站网站内容建设包括什么
  • 素材网站推荐游戏推广好做吗
  • 泰州建站价格wordpress 媒体库 插件
  • 网站建设合同标准版网络营销的效果表现在哪几个方面
  • 一步步教会你怎么做网站芜湖公司做网站
  • 中国企业网站建设现状linux 网站建设模板
  • 石家庄哪里有做外贸网站的公司dede视频网站
  • C 做的窗体怎么变成网站国内免费素材网站
  • 做网站要注册第35类商标吗wordpress 去购买按钮
  • 徐州鸿美网站建设毕业设计购物网站怎么做
  • 网站建设云服务网站模板建网站
  • 网站建设在哪里推广网站建设基础心得
  • 全网营销建设网站兰溪优秀高端网站设计地址
  • 作文网站网址大全如何注册个人营业执照
  • 汽车用品网站规划深圳南山做网站公司
  • 社区微网站建设方案ppt东莞道滘网站建设
  • 河北省建设厅办事大厅网站算命网站搭建
  • 哪个网站做ic好网页设计与应用论文
  • 历史网站怎么做民法典建设工程施工合同