Can I make console.log() print a custom message for a class?

Viewed 47

Similar to Date, I'd like to customize the label printed in the console for my class.

const d = new Date(123);
console.log(d);

// Thu Jan 01 1970 10:00:00 GMT+1000 (Australian Eastern Standard Time)


class MyClass {
  constructor(a, b, c) {
    this._a = a;
    this._b = b;
    this._c = c;
  }
}

const t = new MyClass(1, "2", 3);
console.log(t);

// MyClass {_a: 1, _b: "2", _c: 3}  <----- Can I customize this?

Is there a special function I need to implement to output a different label when calling console.log() ?

p.s:- watch the console of browser to see the Label value format output

0 Answers
Related