ArrayBuffer.prototype.transferToFixedLength()
Baseline
2024
Newly available
Since March 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
transferToFixedLength() は ArrayBuffer インスタンスのメソッドで、このバッファと同じバイト内容を持つサイズ変更不可能な新しい ArrayBuffer を作成し、その後でこのバッファを分離します。
構文
transferToFixedLength()
transferToFixedLength(newByteLength)
引数
newByteLength-
新しい
ArrayBufferのbyteLengthです。デフォルト値はこのArrayBufferのbyteLengthです。newByteLengthがこのArrayBufferのbyteLengthより小さい場合、「オーバーフロー」したバイトは破棄されます。newByteLengthがこのArrayBufferのbyteLengthより大きい場合、余分なバイトはゼロで埋められます。
返値
新しい ArrayBuffer オブジェクトです。その内容は、この ArrayBuffer の内容で初期化され、余分なバイトがある場合はゼロで埋められます。新しい ArrayBuffer は、常にサイズ変更不可能です。元の ArrayBuffer は切り離されます。
例外
TypeError-
この
ArrayBufferが既に分離されている場合、または指定された操作によってのみ分離可能な場合に発生します。現在、GPUBuffer.getMappedRange()やWebAssembly.Memory.bufferなどの特定の Web API のみが、指定された分離メソッドを持つArrayBufferオブジェクトを作成できます。
解説
transfer() とは異なり、transferToFixedLength() は常にサイズ変更不可の ArrayBuffer を作成します。これは、この ArrayBuffer がサイズ変更可能であっても、newByteLength が maxByteLength より大きくできるということです。詳細については、詳細については、ArrayBuffer の移譲を参照してください。
例
>リサイズ可能な ArrayBuffer を固定長のものに移譲
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
const view = new Uint8Array(buffer);
view[1] = 2;
view[7] = 4;
const buffer2 = buffer.transferToFixedLength();
console.log(buffer2.byteLength); // 8
console.log(buffer2.resizable); // false
const view2 = new Uint8Array(buffer2);
console.log(view2[1]); // 2
console.log(view2[7]); // 4
transferToFixedLength を使用する場合、newByteLength は元の ArrayBuffer の maxByteLength よりも大きくすることができます。
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
const view = new Uint8Array(buffer);
view[1] = 2;
view[7] = 4;
const buffer2 = buffer.transferToFixedLength(20);
console.log(buffer2.byteLength); // 20
console.log(buffer2.resizable); // false
const view2 = new Uint8Array(buffer2);
console.log(view2[1]); // 2
console.log(view2[7]); // 4
仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-arraybuffer.prototype.transfertofixedlength> |