【问题标题】:Can't understand what's going on with EventEmitter using webpack and es6 modules无法理解使用 webpack 和 es6 模块的 EventEmitter 发生了什么
【发布时间】:2016-08-31 19:08:09
【问题描述】:

我正在尝试同时学习 webpack 和 es6,并意识到需要某种事件总线。我是 EventEmitters 和构造函数的新手。我已经阅读了文档以及一堆示例,但老实说,我不明白为什么下面的代码会表现得如此。发射器只有一个实例,那么为什么计数器或 removeListener 找不到任何东西呢?如果我将所有代码都放在一个文件中,它可以正常工作。

entry.js

import dVpEe from '../modules/eventEmitter';
const temp = new dVpEe();


var abc = function abc() {
    console.log('funciton a');
};

var b = function() {
    console.log('funciton b');
};

temp.evesdroppers('connection');
temp.hear('connection', abc);
temp.evesdroppers('connection');
temp.say('connection');
temp.hear('connection', b);
temp.evesdroppers('connection');
temp.say('connection');
temp.walk('connection', abc);
temp.say('connection');

eventEmitter.js

import events from 'events';
import util from 'util';

var EventEmitter = events.EventEmitter;

var dVpEe = function() {
    EventEmitter.call(this);
};

util.inherits(dVpEe, EventEmitter);

dVpEe.prototype.walk = function(event, cb, name) {
    console.log('%c' + (name || 'creeper') + '%c NOT listening for %c' + event, 'color: pink', 'color: white', 'color: pink');
    this.removeListener(event, cb);
};

dVpEe.prototype.say = function(event, name) {
    console.log('%c' + (name || 'someone') + '%c screamed %c' + event, 'color: pink', 'color: white', 'color: pink');
    this.emit(name);
};

dVpEe.prototype.evesdroppers = function(event) {
    var eventListeners = events.EventEmitter.listenerCount(this, event);
    console.log('%c' + eventListeners + '%c listner(s) for %c' + event, 'color: pink', 'color: white', 'color: pink');
};

dVpEe.prototype.hear = function(event, cb, name) {
    console.log('%c' + (name || 'creeper') + '%c listening for %c' + event, 'color: pink', 'color: white', 'color: pink');
    this.addListener(name, cb);
};

export default dVpEe;

输出

0 listner(s) for connection
creeper listening for connection
0 listner(s) for connection
someone screamed connection
function a
creeper listening for connection
0 listner(s) for connection
someone screamed connection
function a
funciton b
creeper NOT listening for connection
someone screamed connection
funciton a
funciton b

【问题讨论】:

    标签: javascript node.js ecmascript-6 webpack eventemitter


    【解决方案1】:

    这只是dVpEe.prototype.hear中的一个错字。

    dVpEe.prototype.hear = function(event, cb, name) {
        console.log((name || 'creeper') + ' listening for "' + event + "'");
        this.addListener(name, cb); // ouch!
    };
    

    我还建议将deprecated EventEmitter.listenerCount 替换为emitter.listenerCount

    dVpEe.prototype.evesdroppers = function(event) {
        var eventListeners = this.listenerCount(event);
    };
    

    由于您使用的是 ES6,我建议利用 class 语法,它更具可读性:

    import { EventEmitter } from 'events';
    
    export default class extends EventEmitter {
      walk(event, cb, name) {
          console.log((name || 'creeper') + ' NOT listening for "' + event + "'");
          this.removeListener(event, cb);
      }
    
      // and so on
    };
    

    【讨论】:

    • 您应该添加如何修复this.addListener(name, cb); 而不是只注意“哎哟!
    • omg...我在堆栈上的第一篇文章是一个错字。我花了 2 个小时一遍又一遍地重构它。我会尴尬地躲在角落里闷闷不乐。好吧,至少这意味着我正确地掌握了这个概念,哈哈。感谢您的指出以及建议!只是那些日子之一......
    • 没什么大不了的 :) 如果答案有帮助,请毫不犹豫地投票。
    • 一旦我得到一些代表,我会回来的。
    猜你喜欢
    • 2013-08-26
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2021-05-13
    • 2019-03-03
    相关资源
    最近更新 更多