js equivilant of __repr__ python
// Yes, in node.js you can install the "util" module (nodejs api module)
// Like this
class MyClass {
constructor(text) {
this.text = text;
}
[util.inspect.custom]() {
return this.text;
}
}
const classInstance = new MyClass("Hello World!");
console.log(classInstance); // Will log "Hello World!"
// Keep in mind, this also affects usage in other areas, so make sure to
// only do this when necessary. Or keep tabs on the actual class too
class MyClass {
constructor(text) {
this.text = text;
this.classInstance = this; // This
}
[util.inspect.custom]() {
return this.text;
}
}