Is there a way to tell the stringify method to convert an object to a primitive datatype?
class Bar {
constructor() {
this.name = 'bar';
}
}
const obj = {foo: new Bar};
JSON.stringify(obj);
// output: '{foo: {name: 'bar'}}'
// wanted output: '{foo:'bar'}'
I've tried overriding toSting and valueOf methods but with no results
Bar.prototype.toString = function() {
return this.name;
}
Bar.prototype.valueOf = function() {
return this.name;
}