🔰converting objects

JSvalueobject ⟩ convert

convert an object to other forms.

obj.toString()          // object -> string
obj.toLocaleString()    // localization
obj.valueOf()           // object -> primitive (typically, number)
obj.toJSON()            // customization method used by JSON.stringify()

about .toJSON()

  • Object.prototype does not define.toJSON().

  • JSON.stringify() looks for .toJSON() on any object it is asked to serialize.

  • if it exists, it is invoked, and the return value is serialized, instead of the original object.

Last updated