首页 > 编程知识 正文

typescript编程 pdf,typescript和es6区别

时间:2023-05-06 21:24:33 阅读:151455 作者:1779

核心API方法(静态方法)功能Dispatcher.on监听事件参数0:事件名称,参数1:监听方法的范围一般传递给this,参数2:监听方法参数2:监听方法(可选)删除字段对象所有监听方法Dispathcer.dispatch发送事件参数0:如果不输入事件名称,请选择一个或多个变量参数

案例1:

A类里进行事件监听 和 结束移除事件

classa{start(}{dispatcher.on ) ' game-start ',this,this.onGameStart ); dispatcher.on('game-end ',this,this.onGameEnd ); } onGameStart ()//游戏开始业务逻辑) ongameend ) over? 3360布尔(//游戏在不同情况下退出时事件处理//删除事件结束//dispatcher.off(game-start ',this,this.onGameStart ); //dispatcher.off(game-end )、this、this.onGameEnd ); //如果需要删除此类的所有事件,只需将其传递给thisdispatcher.off(game-end )、this即可。 } } B类里进行派发

单击class B { //UI按钮处理onClickButton () ) { console.log ) (“用户单击了屏幕”); dispatcher.dispatch(game-start ); //游戏结束方法onGameEnd () dispatcher.dispatch ) ' game-end ',true ); console.log ('用户正常离开房间结束游戏) ); //退出按钮onClickQuitGame () dispatcher.dispatch ) ' game-end ',false ); console.log ('用户强制退出游戏) ); }

案例2 闭包的监听和移除

class C { start ()//闭包签名(指针) let func=)=)//游戏开始业务逻辑(//开始事件拦截dispatcher.off ) ' game-start ',this //开始事件侦听dispatcher.on('game-start ',this,func ); //添加结束事件dispatcher.on(game-end ),this,)={ //删除当前类中的所有事件(dispatcher.off ) game-start ),this ) ); }

上源码

exportdefaultclassdispatcher {/* * *事件池*/privatestatic _ handls : { [ key : string ] 3360 { caller 3360 any }, Handl} /**侦听事件* @param event事件名称* @param caller作用域* @param handl回调方法*/public static on (event : string,calller handl : function (: void (if ) undefined==dispatcher._handls[event] ) dispatcher._ handls [ event ] )是} } /** *拦截* @param event事件名称* @param caller作用域* @param handl回调方法*/public static off (event 3360 string,caller 3360 handl 3360 function=undefined (: void (/事件队列获取const list=dispatcher._ handls [ event ] ); if(listlistinstanceofArray(//遍历所有事件for ) (letI=0,count=list.length; i count; I ) { let e=list[i]; //删除对应的事件if (e.caller==caller ) undefined==handl|| handl==e.handl ) ) { list[i]=list[count - 1] list.pop (; } } } } /** *所有事件*/publicstaticremovealllistener () { Dispatcher._handls={}; } /** *发送事件* @param event事件* @param params参数*/public static dispatch (event 3360 string, params3360any () cony ) } } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。