Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Can I put an event emitter in a class node js

I have a this class:

class test {
    constructor(name) {
        this.name = name
    }
}

So I want to have a event in the class that can be called outside.

Here the full file:

const eventLib = require('events');
const myEmitter = new eventLib.EventEmitter();

class test {
    constructor(name) {
        this.name = name
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can either subclass the EventEmitter so that your class has all the same methods as an EventEmitter object or you can put an eventEmitter in your instance data and can use it there.

Here's subclassing it:

const EventEmitter = require('events');

class Test extends EventEmitter {
    constructor(name) {
        super();
        this.name = name
        this.on("hi", () => {
            console.log("got hi: ", this.name);
        });
    }
}


let x = new Test("Bob");
x.emit("hi");
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda