Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';我不明白是什么';使用webpack和es6模块的EventEmitter正在运行_Javascript_Node.js_Ecmascript 6_Webpack_Eventemitter - Fatal编程技术网

Javascript Can';我不明白是什么';使用webpack和es6模块的EventEmitter正在运行

Javascript Can';我不明白是什么';使用webpack和es6模块的EventEmitter正在运行,javascript,node.js,ecmascript-6,webpack,eventemitter,Javascript,Node.js,Ecmascript 6,Webpack,Eventemitter,我试图同时学习webpack和es6,并意识到需要某种事件总线。我是EventEmitter和构造函数的新手。我已经阅读了文档和大量示例,但我真的不明白为什么下面的代码会以这种方式运行。发射器只有一个实例,那么为什么计数器或removeListener不能找到任何东西呢?如果我把所有代码都放在一个文件中,它就可以正常工作 entry.js import dVpEe from '../modules/eventEmitter'; const temp = new dVpEe(); var ab

我试图同时学习webpack和es6,并意识到需要某种事件总线。我是EventEmitter和构造函数的新手。我已经阅读了文档和大量示例,但我真的不明白为什么下面的代码会以这种方式运行。发射器只有一个实例,那么为什么计数器或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;
输出


这只是dVpEe.prototype.hear中的一个输入错误

dVpEe.prototype.hear = function(event, cb, name) {
    console.log((name || 'creeper') + ' listening for "' + event + "'");
    this.addListener(name, cb); // ouch!
};
我还建议将
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
};

这只是dVpEe.prototype.hear中的一个输入错误

dVpEe.prototype.hear = function(event, cb, name) {
    console.log((name || 'creeper') + ' listening for "' + event + "'");
    this.addListener(name, cb); // ouch!
};
我还建议将
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)而不是只注意“哎哟!”天哪。。。我在stack上的第一篇文章是一个打字错误。我花了2个小时反复重构这个。我要尴尬地在角落里生气。嗯,至少这意味着我正确地理解了这个概念,哈。感谢您的指点和建议!只是其中的一天……没什么大不了的:)如果答案有帮助,请毫不犹豫地投票。我会在获得一些代表后再回来讨论。你应该添加如何修复
this.addListener(name,cb)而不是只注意“哎哟!”天哪。。。我在stack上的第一篇文章是一个打字错误。我花了2个小时反复重构这个。我要尴尬地在角落里生气。嗯,至少这意味着我正确地理解了这个概念,哈。感谢您的指点和建议!只是其中的一天……没什么大不了的:)如果答案有帮助,请毫不犹豫地投赞成票。一旦我得到一些代表,我会回来的。