2016-05-05 15 views
0

webpackとes6を同時に学習しようとしていて、イベントバスのようなものが必要です。私はEventEmittersとコンストラクタが初めてです。私は、ドキュメントやよくある例として読んだことがありますが、正に、以下のコードがそのように動作する理由はわかりません。エミッタのインスタンスが1つしかないので、何も見つからないカウンタやremoveListenerを使用しないのはなぜですか?私はすべてのコードを1つのファイルに持っていればうまくいきます。webpackとes6モジュールを使用してEventEmitterで何が起こっているのか理解できません

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

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 

答えて

2

ouputを

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! 
}; 

私もemitter.listenerCountdeprecatedEventEmitter.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 
}; 
+2

あなたは"* ouch!*"に気づくのではなく、 'this.addListener(name、cb);'を修正する方法を追加する必要があります – Bergi

+0

omg ...スタック上の私の最初の投稿は誤字です。私は2時間以上これを何度もリファクタリングするのに過ごしました。私は恥ずかしがり屋の隅にこぼれ落ちるつもりです。少なくとも私はコンセプトを正確に把握しています。それを指摘していただきありがとうございます。当時のちょうど1つ... – Jeff

+0

それは大したことではありません:)答えが役に立ったらupvoteに躊躇しないでください。 –

関連する問題