gpt4 book ai didi

javascript - 从类中发出事件

转载 作者:搜寻专家 更新时间:2023-10-31 23:51:45 26 4
gpt4 key购买 nike

是否可以从类中发出事件?

class MyEvent extends EventEmitter {
constructor(options) {
super();
this.options = options;
this.execute();
}

execute() {
// do something;
this.emit("close"); // Pass here to the instance.
}
}

然后:

const myEvent = MyEvent({});
myEvent.on("close", (data) => {
// do something here.
});

当前没有从类中调用关闭事件。但是这里=>https://coligo.io/nodejs-event-emitter/自从上类。

最佳答案

可能是因为你在构造函数中调用了execute。它被调用,但您的事件处理程序在构造对象后被附加。附加事件处理程序后尝试调用 myEvent.execute() 以查看它是否被调用。

关于javascript - 从类中发出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43059258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com