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

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

海外散仙厉飞雨 · 真仙发布于
7

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 · 金仙后期

    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');

学员
dechuangchang · 金仙后期君君儿🐭 · 真仙lgz · 金仙后期钟声 · 道祖lilili001 · 金仙后期eowyn · 金仙巅峰heyengineer · 真仙foreverhot · 真仙修 · 真仙zjacker · 真仙薄情人 · 道祖00Jane · 金仙后期ttswh2009 · 太乙巅峰kelly · 大罗samuelshang · 大罗Fmain · 真仙itlaowang520 · 太乙巅峰wangzhe123 · 太乙巅峰mike652638 · 真仙iosLongFeng · 太乙巅峰Super-Ps · 金仙后期与UIuoi89898 · 道祖qiaochunmei · 真仙qianduan · 真仙深lin人不知 · 真仙liaokui · 真仙Geassu · 道祖lihoubai · 真仙sishenguodong · 金仙后期zbx7858 · 道祖
最新动态
dechuangchang · 金仙后期学习到了7:00
君君儿🐭 · 真仙学习到了0:00
lgz · 金仙后期学习到了7:00
钟声 · 道祖学习到了7:29
lilili001 · 金仙后期学习到了7:21

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

Top