Refering to this post : Why console.log(obj) show structure of obj instead of return value of toString()
I have figured out that this piece of code will log the value returned by toString()
method of Obj
class Obj {
constructor(){
this.prop = 'a property';
}
toString(){
return 'This is toString method of an instance of Obj and I have ' + this.prop;
}
}
console.log(new Obj() + "");
Is there another approach to do that ?