//导入内置模块let EventEmitter = require('events'); let util=require('util');//Man继承EventEmitterutil.inherits(Man,EventEmitter); //创建一个函数function Man(){}//实例化函数let man=new Man();function findGirl() { console.log('找新的女朋友') }function saveMoney() { console.log('省钱') } man.on('失恋',findGirl)//失恋 ,绑定一个函数方法man.on('失恋',saveMoney)//失恋 ,绑定一个函数方法man.removeListener('失恋',findGirl); //移除省钱man.emit('失恋');
The above is the detailed content of Node.js publish-subscribe mode method. For more information, please follow other related articles on the PHP Chinese website!