> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/obj/convert.md).

# converting objects

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [object](/web/js/val/obj.md) ⟩ convert

{% hint style="success" %}
convert an object to other forms.

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

{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="info" %} <mark style="color:yellow;">**about**</mark>**&#x20;**<mark style="color:purple;">**.toJSON()**</mark>

* <mark style="color:blue;">**Object.prototype**</mark> <mark style="color:yellow;">**does**</mark>**&#x20;**<mark style="color:red;">**not**</mark> define<mark style="color:yellow;">`.toJSON()`</mark>.&#x20;
* [**JSON.stringify()**](/web/js/val/obj/convert/serializing-objects.md) looks for <mark style="color:yellow;">`.toJSON()`</mark> on any object it is asked to serialize.
* if it exists, it is invoked, and the <mark style="color:orange;">**return value**</mark>**&#x20;**<mark style="color:yellow;">**is serialized**</mark>, instead of the original object.
  {% endhint %}
  {% endtab %}

{% tab title="🔴 主題" %}

* [serializing objects](/web/js/val/obj/convert/serializing-objects.md)
* [object -> primitive conversion](/web/js/val/obj/convert/obj-to-prim.md)
  * :m: [.toString()](/web/js/val/obj/convert/.tostring.md)&#x20;
  * :m: [.valueOf()](/web/js/val/obj/convert/.valueof.md)
  * :floppy\_disk: [valueToString()](/web/js/val/obj/convert/valuetostring.md)
    {% endtab %}

{% tab title="👥 相關" %}

* [type conversion](/web/js/val/type/convert.md)
  {% endtab %}

{% tab title="💈範例" %}

* replit ⟩ [.toJSON()](https://replit.com/@pegasusroe/toJSON#index.js)

```javascript
let point = {
    x: 1, y: 2,
    toJSON() { return `(${this.x}, ${this.y})` }    // custom .toJSON() method
};

JSON.stringify([point]),    // '["(1, 2)"]'
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩&#x20;
  * [ ] 3.9.2 Explicit Conversions
  * [ ] 6.9 Object Methods
  * [ ] 14.4.7
    {% endtab %}

{% tab title="📘 手冊" %}

* .toString() ⟩&#x20;
  * [Object.prototype.toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString)
* .toLocaleString() ⟩&#x20;
  * [Object.prototype.toLocaleString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString)
  * [Date.prototype.toLocaleString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString)
* .valueOf() ⟩&#x20;
  * [Object.prototype.valueOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf)
* .toJSON() ⟩&#x20;
  * [Date.prototype.toJSON()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON)
    {% endtab %}
    {% endtabs %}
