TypedArray.prototype.with()

Baseline 2023

Newly available

Since July 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

TypedArray 实例的 with() 方法是使用 括号表示法 更改给定索引的值的 copying 版本。它返回一个新的类型化数组,其中给定索引处的元素替换为给定值。该方法与 Array.prototype.with() 的算法相同。

¥The with() method of TypedArray instances is the copying version of using the bracket notation to change the value of a given index. It returns a new typed array with the element at the given index replaced with the given value. This method has the same algorithm as Array.prototype.with().

语法

¥Syntax

js
arrayInstance.with(index, value)

参数

¥Parameters

index

用于更改类型化数组 转换为整数 的从零开始的索引。

value

要分配给给定索引的任何值。

返回值

¥Return value

一个新的类型化数组,其中 index 处的元素替换为 value

¥A new typed array with the element at index replaced with value.

例外情况

¥Exceptions

RangeError

如果是 index >= array.lengthindex < -array.length,则抛出。

描述

¥Description

详细信息请参见 Array.prototype.with()。此方法不是通用的,只能在类型化数组实例上调用。

¥See Array.prototype.with() for more details. This method is not generic and can only be called on typed array instances.

示例

¥Examples

使用 with()

¥Using with()

js
const arr = new Uint8Array([1, 2, 3, 4, 5]);
console.log(arr.with(2, 6)); // Uint8Array [1, 2, 6, 4, 5]
console.log(arr); // Uint8Array [1, 2, 3, 4, 5]

规范

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.with

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看