The MDN documentation on Object.prototype.toString
says that when toString
gets overriden, it should only return a primitive value:
The
toString()
function you create must return a primitive, otherwise it will be ignored.
However, in the following example we return an object inside of toString
and it returns the object normally:
const ob2 = {
val1: 100,
val2: 200,
toString: function() {
return {
veh: "meh"
}; // Object.
}
};
console.log(ob2.toString());
Output:
{ veh: "meh" }
So the toString
method returns the object normally. What is up with the misinformation?