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

Node.js publish-subscribe model example explanation

巴扎黑
Release: 2017-09-11 09:46:13
Original
1536 people have browsed it

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('失恋');
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template