Nodejs 自定义类继承EventEmitter

首先看下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‘)

Nodejs 自定义类继承EventEmitter

上一篇:JavaWeb项目中引入spring框架


下一篇:php支付宝提现开发流程