SharedArrayBuffer.prototype.grow()

SharedArrayBuffer 实例的 grow() 方法将 SharedArrayBuffer 增长到指定大小(以字节为单位)。

¥The grow() method of SharedArrayBuffer instances grows the SharedArrayBuffer to the specified size, in bytes.

语法

¥Syntax

js
grow(newLength)

参数

¥Parameters

newLength

SharedArrayBuffer 大小调整为的新长度(以字节为单位)。

返回值

¥Return value

无 (undefined)。

¥None (undefined).

例外情况

¥Exceptions

TypeError

如果 SharedArrayBuffer 不可增长,则抛出该异常。

RangeError

如果 newLength 大于 SharedArrayBuffermaxByteLength 或小于 byteLength,则抛出。

描述

¥Description

grow() 方法将 SharedArrayBuffer 增长到 newLength 参数指定的大小,前提是 SharedArrayBuffergrowable,并且新大小小于或等于 SharedArrayBuffermaxByteLength。新字节被初始化为 0。

¥The grow() method grows a SharedArrayBuffer to the size specified by the newLength parameter, provided that the SharedArrayBuffer is growable and the new size is less than or equal to the maxByteLength of the SharedArrayBuffer. New bytes are initialized to 0.

示例

¥Examples

使用增长()

¥Using grow()

在此示例中,我们创建一个可增长到最大长度 16 字节的 8 字节缓冲区,然后检查其 growable 属性,如果 growable 返回 true,则增长它:

¥In this example, we create a 8-byte buffer that is growable to a max length of 16 bytes, then check its growable property, growing it if growable returns true:

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

if (buffer.growable) {
  console.log("SAB is growable!");
  buffer.grow(12);
}

规范

Specification
ECMAScript Language Specification
# sec-sharedarraybuffer.prototype.grow

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看