Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!