Home  >  Article  >  Web Front-end  >  Node.js publish-subscribe model example explanation

Node.js publish-subscribe model example explanation

巴扎黑
巴扎黑Original
2017-09-11 09:46:131527browse

The editor below will bring you an example of the node.js publish-subscribe model. The editor thinks it’s pretty good, and now I want to give it to you and give it as a reference. Let’s follow the editor and take a look.

The examples are as follows:


//导入内置模块
let EventEmitter = require('events');
let util=require('util');
//Man继承EventEmitter
util.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 model example explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn