📜  节点 js 事件发射器 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:32.260000             🧑  作者: Mango

代码示例1
const EventEmitter = require('events');

class MyEmitter extends EventEmitter {
  foo() {
    this.emit('test');
  }
}
const myEmitter = new MyEmitter();
myEmitter.on('test', () => console.log('Yay, it works!'));

myEmitter.foo();    // Yay it works!