0

I have a scenario where in I want to print custom message on log of some method reference.

Say I have a function sum(){} now on console.log(sum); I want to log a custom message on console. How can we achieve that?

Gautam
  • 815
  • 6
  • 15
  • See https://stackoverflow.com/questions/6307514/is-it-possible-to-override-javascripts-tostring-function-to-provide-meaningfu – Thierry Oct 20 '17 at 18:25
  • 1
    Possible duplicate of [Is it possible to override JavaScript's toString() function to provide meaningful output for debugging?](https://stackoverflow.com/questions/6307514/is-it-possible-to-override-javascripts-tostring-function-to-provide-meaningfu) – Thierry Oct 20 '17 at 18:25
  • @Thierry yes, it is possible to do that. You simply add a `toString()` method to the function. – code_monk Oct 20 '17 at 18:36
  • @Thierry This question is different from that as I am trying to log function reference itself not the object created from it. – Gautam Oct 20 '17 at 18:37

1 Answers1

1

Like this

var sum = function(){};

sum.toString = function(){ return 'my custom message'; };

console.log(sum);
code_monk
  • 9,451
  • 2
  • 42
  • 41