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

175
Visualizações
¿Cómo utilizar este emisor de eventos basado en funciones?

En esta respuesta anterior , existe el siguiente emisor de eventos basado en funciones.

Tal vez sea súper obvio cómo usarlo, pero no para mí, así que realmente agradecería si alguien pudiera hacer un ejemplo de hola mundo con él.

¿Alguien puede averiguar cómo usarlo?

 function EventBase() { this.events = {}; }; EventBase.prototype = { on: function (event, listener) { if (typeof event !== 'string') throw new TypeError("Event must be a string"); if (typeof event !== 'string') throw new TypeError("Listener must be a function"); this.events[event] || (this.events[event] = []); this.events[event].push(listener); }, off: function (event, listener) { if (arguments.length === 0) { // remove all listeners this.events = {}; return; } if (!this.events[event]) { // return if there's no event by the given name return; } if (arguments.length === 1) { // remove all listeners for the given event delete this.events[event]; return; } // remove specific listener this.events[event] = this.events[event].filter(function (func) { return func !== listener; }); }, emit: function (event) { if (!this.events[event]) { // return if there's no event by the given name return; } // get args var args = [].slice.call(arguments, 1); // invoke listeners this.events[event].forEach(listener => listener.apply(this, args)); }, getListenerCount: function (event) { // get total number of listeners if (arguments.length === 0) { return Object.keys(this.events).reduce((count, key) => count + this.getListenerCount(key), 0); } // return zero for non-existing events if (!this.events[event]) { return 0; } // return count for specific event return this.events[event].length; } };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

EventBase es una función constructora. Después de instanciarlo, podemos llamar a los métodos definidos en el prototipo. El método le permite agregar una devolución on llamada de escucha a un determinado nombre de evento. emit permite emitir un nombre de evento, que luego activará el oyente escuchando ese evento

 const event = new EventBase() event.on("connect", () => { console.log("connected") }) event.emit("connect")
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