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

97
Visualizações
How to override a function in a class being returned from module in javascript

I have a logging class in a module which contains a static function to return an instance of itself, so a simplified version looks like this:

Class Logger {

    static createFromConfig(key) {   
      return new Logger(key);
    }

    info(message) {
      // do logging stuff
    }

    debug(message) {
      // do logging stuff
    }

    error(message) {
      // do logging stuff
    }
}

This is used in many places throughout our codebase like:

    import logging from 'logging';
    const log = logging.Logger.createFromConfig('keyname');
    log.info('App starting');

In one project I wish to add some extra functionality to the error function, without having to change code in any of the places calling it. So something like

log.error = () => {
    // do my extra stuff here
    log.error() // call the original error function
}

I've tried extending the class like below but I think the problem is the original base class just gets returned from the static function so my overridden function isn't called:

class dblogger extends logging.Logger {
  error(...args) {
   // do my extra stuff here 
    super.error();
  }
}

const dblogging = {
  Logger: dblogger
};

export default dblogging;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to call the original method from the Logger prototype.

function getdblogger(keyname) {
    const log = logging.Logger.createFromConfig(keyname);
    log.error = function(message) {
        // do my extra stuff here
        Logger.prototype.error.call(this, message) // call the original error function
    }
    return log;
}

Note that you have to use an ordinary function rather than an arrow function so it gets this.

If you can get changes made to the original Logger class, you could chage createFromConfig() to allow the class to be specified, then you could use your subclass.

Class Logger {

    static createFromConfig(key, loggerClass = Logger) {   
      return new loggerClass(key);
    }

    info(message) {
      // do logging stuff
    }

    debug(message) {
      // do logging stuff
    }

    error(message) {
      // do logging stuff
    }
}

Then in your files:

const log = logging.Logger.createFromConfig('keyname', dblogger);
about 4 years ago · Juan Pablo Isaza Relatório

0

I believe you really want to use this.constructor.prototype.error within your method:

class Logger{
  static createFromConfig(key){   
    return new Logger(key);
  }
  info(message){
    // do logging stuff
  }
  debug(message){
    // do logging stuff
  }
  error(message){
    return message;
    // do logging stuff
  }
}
class logging extends Logger{
}
const log = logging.createFromConfig('test');
log.error = function(message){
  console.log(this.constructor.prototype.error('Logger message here'));
  console.log(message);
}
log.error('within log');

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