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

[zh-cn]: add the translation of ArrayBuffer.resize() #17095

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: ArrayBuffer.prototype.resize()
slug: Web/JavaScript/Reference/Global_Objects/ArrayBuffer/resize
---

{{JSRef}}

{{jsxref("ArrayBuffer")}} 实例的 **`resize()`** 方法将 `ArrayBuffer` 调整为指定的大小,以字节为单位。

{{EmbedInteractiveExample("pages/js/arraybuffer-resize.html")}}

## 语法

```js-nolint
resize(newLength)
```

### 参数

- `newLength`
- : `ArrayBuffer` 要调整到的新的长度,以字节为单位。

### 返回值

无({{jsxref("undefined")}})。

### 异常

- {{jsxref("TypeError")}}
- : 如果 `ArrayBuffer` 已分离或不可调整大小,则抛出该错误。
- {{jsxref("RangeError")}}
- : 如果 `newLength` 大于该 `ArrayBuffer` 的 {{jsxref("ArrayBuffer/maxByteLength", "maxByteLength")}},则抛出该错误。

## 描述

`resize()` 方法将 `ArrayBuffer` 调整为 `newLength` 参数指定的大小,前提是该 `ArrayBuffer` 是[可调整大小的](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/resizable)并且新的大小小于或等于该 `ArrayBuffer` 的 {{jsxref("ArrayBuffer/maxByteLength", "maxByteLength")}}。新字节被初始化为 0。

请注意,你可以使用 `resize()` 来缩小和增大 `ArrayBuffer`——即使 `newLength` 小于 `ArrayBuffer` 的当前 {{jsxref("ArrayBuffer/byteLength", "byteLength")}}。

## 示例

### 使用 resize()

在这个示例中,我们创建一个 8 字节缓冲区,该缓冲区可调整大小到的最大长度是 16 字节,然后检查其 `resizable` 属性,如果 `resizable` 返回 `true` 则调整其大小:

```js
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });

if (buffer.resizable) {
console.log("缓冲区大小是可调整的!");
buffer.resize(12);
}
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- {{jsxref("ArrayBuffer")}}
- {{jsxref("ArrayBuffer.prototype.resizable")}}
- {{jsxref("ArrayBuffer.prototype.maxByteLength")}}