Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toJSON() instance method page for DOMRectReadOnly #36701

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions files/en-us/web/api/domrectreadonly/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ The **`DOMRectReadOnly`** interface specifies the standard properties (also used
- {{domxref("DOMRectReadOnly/fromRect_static", "DOMRectReadOnly.fromRect()")}}
- : Creates a new `DOMRectReadOnly` object with a given location and dimensions.

## Instance methods

- {{domxref("DOMRectReadOnly.toJSON()")}}
- : Returns a JSON representation of the `DOMRectReadOnly` object.

## Specifications

{{Specifications}}
Expand Down
48 changes: 48 additions & 0 deletions files/en-us/web/api/domrectreadonly/tojson/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "DOMRectReadOnly: toJSON() method"
short-title: toJSON()
slug: Web/API/DOMRectReadOnly/toJSON
page-type: web-api-instance-method
browser-compat: api.DOMRectReadOnly.toJSON
---

{{APIRef("Geometry Interfaces")}}{{AvailableInWorkers}}

The {{domxref("DOMRectReadOnly")}} method
`toJSON()` returns a
{{Glossary("JSON")}} representation of the `DOMRectReadOnly` object.
wbamberg marked this conversation as resolved.
Show resolved Hide resolved

## Syntax

```js-nolint
toJSON()
```

### Parameters

None.

### Return value

A new object whose properties are set to the values in the
`DOMRectReadOnly` on which the method was called.
wbamberg marked this conversation as resolved.
Show resolved Hide resolved

## Examples

This example creates a {{domxref("DOMRectReadOnly")}} that represents a rectangle at position `(10, 20)` with a width of `100` and a height of `50`. It then calls `toJSON()` to obtain a JSON representation of the rectangle.

```js
const rect = new DOMRectReadOnly(10, 20, 100, 50);

const rectJSON = rect.toJSON();
console.log(rectJSON);
// Output: { x: 10, y: 20, width: 100, height: 50, top: 20, right: 110, bottom: 70, left: 10 }
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}