Skip to content

tcstory/event-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

event-emitter

仿写nodejsevents.EventEmitter,实现了其中的大部分方法. 可以用的方法如下所示(具体用法和nodejs基本一样):

  • on(event:string, callback:Function):boolean
  • once(event:string, callback:Function:boolean
  • emit(event:string, ...args:Array<any>):boolean
  • listenerCount(event:string):number
  • removeListener(event:string, func:Function):boolean
  • removeAllListener(event:string):boolean
  • setDefaultListenerCount(n:number=10):boolean
  • getDefaultListenerCount():number

传入参数

var ev = new EventEmitter();
ev.on('test',function(x,y,z){
        console.log(x+y+z)
    }
)
ev.emit('test',1,2,3)
==> 6

简单的来说,就是传给事件处理程序的参数,会被收集到一个变量中

提示

  • 不同于nodejs里的events.EventEmitter,使用该类注册的事件处理程序中的this并不会指向EventEmitter的实例(具体请看nodejs文档)

About

发布/订阅

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published