簡體   English   中英

EventEmitter沒有發射

[英]EventEmitter has no emit

我有這個節點js腳本:

var EventEmitter = require('events').EventEmitter,
    util = require('util');

var pfioObject = function () {
    this.init = function () {
        console.log("started server");
    };
    this.deinit = function () {
        console.log("stopped server");
    };
    this.read_input = function () {
        return 0;
    };
};
console.log(util.inspect(EventEmitter, false, null)); //<--- this shows no method emit either

var pfio = new pfioObject();

var pfioServer = function () {
    this.prev_state = 0;
    this.start = function () {
        pfio.init();
        this.watchInputs();
    };

    this.stop = function () {
        pfio.deinit();
    };
}

util.inherits(pfioServer, EventEmitter);

// add some event emitting methods to it
pfioServer.prototype.watchInputs = function () {
    var state = pfio.read_input();

    if (state !== this.prev_state) {
        this.emit('pfio.inputs.changed', state, this.prev_state);
        this.prev_state = state;
    }

    setTimeout(this.watchInputs, 10); // going to put this on the event loop next event, more efficient
};

// export the object as a module
module.exports = new pfioServer();

出於某種原因,節點錯誤表明沒有發出對象,我已經執行了npm install events以查看是否可以解決該問題,但沒有。 我不確定為什么會收到此錯誤。

我認為代碼中的某個地方有錯誤,但是什么也看不到。

要運行此代碼,我還有另一個腳本可以執行此操作:

var pfioServer = require('./pfioServer'),
util = require('util');

console.log(util.inspect(pfioServer, false, null)); //<--- this line shows the pfioServer with out the prototype function watchInputs

pfioServer.start();

編輯

我想我可能已經錯過了有關事件發射器內容的一些重要代碼,正在研究事件發射器類的實例化

輕微變化

因此,我不是通過繼承EventEmitter而是通過執行var emitter = new EventEmitter()實例化它

然后在那之后,我在util.inspect關於對象必須是對象或為null的錯誤。

還沒有成功。

原因是此行:

setTimeout(this.watchInputs, 10);

觸發超時並this.watchInputs ,它失去了上下文。

您需要明確設置上下文對象(換句話說,什么this里面的方法應指向),用於利用電話bind

setTimeout(this.watchInputs.bind(this), 10);

我發現必須更改兩個內容,第一個代碼塊才能正確運行:

  • 顯式實例化events.EventEmitter()

var events = require('events'); var EventEmitter = new events.EventEmitter();

  • 將調用更改為util.inherits

util.inherits(pfioServer, events.EventEmitter);

使服務器運行的最后一件事是robertklep寫道:修復setTimeout() 綁定

setTimeout(this.watchInputs.bind(this), 10);
 console.log(util.inspect(EventEmitter, false, null)); // this shows no method emit either 

EventEmitter構造函數沒有這種方法。 EventEmitter.prototype有。 要打印(不可枚舉的) .prototype屬性,可以使用util.inspect(EventEmitter, {showHidden: true})作為注釋中提到的@Emissary。

 // this line shows the pfioServer with out the prototype function watchInputs console.log(util.inspect(pfioServer, false, null)); 

是。 util.inspect不顯示繼承的屬性,僅顯示在pfioServer實例本身上明顯的屬性。

 setTimeout(this.watchInputs, 10); // going to put this on the event loop next event, more efficient 

我寧願說它可以防止無限循環……但是,這也會在下一次調用中破壞this上下文,而不再是您的pfioServer實例。 請參閱如何在回調中訪問正確的`this`上下文?

但是,在您的情況下,我認為沒有必要使用構造函數並從原型繼承。 您只是在導出一個單例對象,您可以簡單地使用對象文字實例化該對象,並通過其變量名稱進行引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM