TypedArray.prototype.with()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2023年7月.
TypedArray 实例的 with() 方法是使用方括号表示法修改指定索引值的复制方法版本。它会返回一个新的类型化数组,其指定索引处的元素值会被新值替换。该方法的算法与 Array.prototype.with() 相同。
语法
js
arrayInstance.with(index, value)
参数
返回值
一个新的类型化数组,其中位于 index 处的元素已被 value 替换。
异常
RangeError-
index >= array.length或index < -array.length时抛出。
描述
详情请参见 Array.prototype.with()。该方法不是通用的,只能在类型化数组实例上调用。
示例
>使用 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® 2026 Language Specification> # sec-%typedarray%.prototype.with> |