Answers for "js equivilant of __repr__ python"

1

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;
  }
}
Posted by: Guest on June-02-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language