首先看下EventEmitter的简单是方式
let events = require(‘events‘)
let EventEmitter = events.EventEmitter
let test = new EventEmitter()
test.on(‘message‘,_ => {
console.log(‘response message emitter‘)
})
test.emit(‘message‘)
自定义继承EventEmitter的类
let events = require(‘events‘)
let EventEmitter = events.EventEmitter
class People extends EventEmitter {
constructor(name){
spuer()
this.name = name
this.on(‘message‘,_ => { // 初始化定义
console.log(‘response message‘)
})
}
}
let p1 = new People(‘panda‘)
P1.on(‘otherMessgae‘,_ => {
console.log(‘response other message‘)
})
p1.emit(‘message‘)