Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda