'What does it mean that `toString` gets “ignored” if it returns a non-primitive?
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
