剧场模式
首页后端Nodejs轻松学 nodejs - 基础篇

轻松学 Node.js - 基础篇 #5 事件

求知小风 · 真仙发布于
9

https://nodejs.org/dist/latest-v9.x/docs/api/events.html

https://nodejs.org/dist/latest-v9.x/docs/api/util.html#util_util_inherits_constructor_superconstructor

var events = require('events');
var util = require('util');

var Person = function(name) {
    this.name = name
}

util.inherits(Person, events.EventEmitter);

var xiaoming = new Person('xiaoming');
var lili = new Person('lili');
var lucy = new Person('lucy');

var person = [xiaoming, lili, lucy];

person.forEach(function(person) {
    person.on('speak', function(message) {
        console.log(person.name + " said: " + message);
    })
})

xiaoming.emit('speak', 'hi');
lucy.emit('speak', 'I want a curry');

// var myEmitter = new events.EventEmitter();

// myEmitter.on('someEvent', function(message) {
//     console.log(message);
// })

// myEmitter.emit('someEvent', 'the event was emitted');
8 条回复
  • pyyzcwg2833 · 元婴
    pyyzcwg2833 · 元婴 #1

    最好用ES6哈哈哈

  • 求知小风 · 真仙

    实践课会用 es6

  • ByeWord · 合体
    ByeWord · 合体 #3

    let events = require('events');
    let utils = require('util');

    // let myEmitter = new events.EventEmitter();

    // myEmitter.on('connected',(message)=>{
    // console.log(message);
    // })
    //按监听器的注册顺序,同步地调用每个注册到名为 eventName 事件的监听器,并传入提供的参数。
    // myEmitter.emit('connected','你好世界')

    class Person extends events.EventEmitter {
    constructor(name){
    super();
    this.name = name;
    }
    }
    let lily = new Person('Lily');
    let lucy = new Person('Lucy');

    let peoples = [lily, lucy];
    peoples.forEach(people => {
    people.on('say',(message)=>{
    console.log(message,this);
    })
    });
    lily.emit('say','你好啊,世界');
    lucy.emit('say','欢迎,大千世界')

  • dechuangchang · 合体

    const EventEmitter = require('events');
    class MyEmitter extends EventEmitter {}

    const fun1= new MyEmitter();
    const fun2= new MyEmitter();
    const fun3= new MyEmitter();

    let arr = [fun1,fun2,fun3];
    arr.forEach((item)=>{
    item.on('event', msg => {
    console.log(msg);
    });
    })
    fun1.emit('event','fun1');
    fun2.emit('event','fun2');
    fun3.emit('event','fun3');

  • DoubleF · 元婴
    DoubleF · 元婴 #5

    这章我基本上看得一头雾水~

  • ll199312 · 元婴
    ll199312 · 元婴 #6

    讲的很棒

  • carrieguo · 元婴
    carrieguo · 元婴 #7

    const EventEmitter = require('events');

    class Person extends EventEmitter {
    constructor(name) {
    super();
    this.name = name;
    }
    }

    let xiaoming = new Person('xiaoming');
    let lili = new Person('lili');
    let lucy = new Person('lucy');

    let person = [xiaoming, lili, lucy];

    person.forEach(function(person) {
    person.on('speak', function(message) {
    console.log(person.name + "say" + message);
    })
    });

    xiaoming.emit('speak', 'hi');

  • Lyle101 · 元婴
    Lyle101 · 元婴 #8


    var events = require('events');
    // util是一个node核心工具库,包含很多功能
    var util = require('util');

    var Person = function(name) { // 定义一个Person类
    this.name = name
    }

    // Person 继承于 events.EventEmitter
    util.inherits(Person, events.EventEmitter);

    // 新建三个对象
    var xiaoming = new Person('xiaoming');
    var lili = new Person('lili');
    var lucy = new Person('lucy');

    var person = [xiaoming, lili, lucy];

    person.forEach(function(person) {
    // 给每个人绑定一个事件
    person.on('speak', function(message) {
    console.log(person.name + " said: " + message);
    })
    })

    // 触发事件:
    xiaoming.emit('speak', 'hi');
    lucy.emit('speak', 'I want a curry');

加微信(qiuzhi99666)入群官方服务号
随机课程
轻松学 Webpack 4 视频教程

轻松学 Webpack 4 视频教程

36 个视频3 小时 44 分钟入门

Pro¥ 119.00¥ 95.20

Webpack已完结

学员(131)
张琳 · 道祖牛顿与🍎 · 元婴vaveee · 道祖zsh19950920 · 真仙陈太太先生 · 太乙金陵小牛 · 元婴Clive · 元婴midoriya · 元婴hanruiying · 道祖帅神orz · 大乘TaiheChen · 合体YQP · 元婴
最新动态
张琳 · 道祖学习到了7:25
牛顿与🍎 · 元婴学习到了3:07
vaveee · 道祖学习到了7:38
zsh19950920 · 真仙学习到了7:37
陈太太先生 · 太乙学习到了7:37
统计信息
    学员: 15785
    视频数量: 918
    帖子数量: 406

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 在线学员:148

Top