我發射的事件就是不想發射。我是nodejs的新成員,對於錯誤的錯誤感到抱歉,但是我幾個小時都無法解決它。發射事件不會發射
客戶端模塊
var Client = require('steam');
var EventEmitter = require('events').EventEmitter;
var newClient = function(user, pass){
EventEmitter.call(this);
this.userName = user;
this.password = pass;
var newClient = new Client();
newClient.on('loggedOn', function() {
console.log('Logged in.'); // this work
this.emit('iConnected'); // this don't work
});
newClient.on('loggedOff', function() {
console.log('Disconnected.'); // this work
this.emit('iDisconnected'); // this don't work
});
newClient.on('error', function(e) {
console.log('Error'); // this work
this.emit('iError'); // this don't work
});
}
require('util').inherits(newClient, EventEmitter);
module.exports = newClient;
app.js
var client = new newClient('login', 'pass');
client.on('iConnected', function(){
console.log('iConnected'); // i can't see this event
});
client.on('iError', function(e){
console.log('iError'); // i can't see this event
});
從哪裏來的「客戶端」模塊?在var Client = require('client'); –
該模塊有效。我的意思是他的事件(loggedOn,loggedOff,錯誤)的作品。所以現在我想進一步傳送它們。 – SLI
很難測試你在做什麼,因爲我不是很明白什麼是,但是我可以在這裏看到兩件事情,當你使用「var newClient = new Client(嘗試在構造函數中使用相同名稱的newClient );」並且可能你在事件監聽器函數內部放棄了這個範圍。可能這可以幫助http://stackoverflow.com/questions/19457294/class-loses-this-scope-when-calling-prototype-functions-by-reference –