0

I am trying to customize how an instance is printed in a console.log statement by default:

function Vehicle(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.speed = 0;
    this.engineOn = false;
}

Vehicle.prototype.valueOf = function() {
    return 'print this';
}

let v = new Vehicle(1,2,3);
console.log(v);

It seems there is a valueOf and a toString function, but neither of those seem to change the value. How would I properly change the default format when it's cast to a string in the console.log statement?

David542
  • 104,438
  • 178
  • 489
  • 842
  • See also the entire thread 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) – ggorlen Feb 04 '22 at 01:47
  • @ggorlen yea that might be the simplest, just doing `console.log('' + v)` to cast it. – David542 Feb 04 '22 at 02:09

0 Answers0