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.

ArrayBuffer 인스턴스의 transferToFixedLength() 메서드는 이 버퍼의 내용과 동일한 바이트의 크기 조절이 불가능한 ArrayBuffer를 새로 생성하고 이 버퍼를 분리합니다.

구문

js
transferToFixedLength()
transferToFixedLength(newByteLength)

매개변수

newByteLength

ArrayBufferbyteLength입니다. 기본값은 이 ArrayBufferbyteLength입니다.

  • newByteLength가 이 ArrayBufferbyteLength보다 작으면 해당 버퍼에 담지못해 넘치는 바이트는 삭제됩니다.
  • newByteLength가 이 ArrayBufferbyteLength보다 크다면 남는 바이트는 0으로 채워집니다.

반환 값

새로운 ArrayBuffer 객체. 해당 객체의 내용은 이 ArrayBuffer의 내용으로 초기화되며, 여분의 바이트가 있다면 0으로 채워집니다. 새로운 ArrayBuffer는 언제나 크기 조절이 불가능합니다. 원본 ArrayBuffer는 분리됩니다.

예외

TypeError

ArrayBuffer가 이미 분리되었다면 발생합니다.

설명

transfer()와 달리, transferToFixedLength()는 항상 크기 조정이 불가능한 ArrayBuffer를 생성합니다. 즉, 이 ArrayBuffer가 크기 조정이 가능하더라도 newByteLengthmaxByteLength보다 클 수 있습니다. 자세한 내용은 ArrayBuffer 전송하기를 참조하세요.

예제

고정 길이 버퍼에 크기 조정 가능한 ArrayBuffer 전송하기

js
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는 원본 ArrayBuffermaxByteLength보다 클 수 있습니다.

js
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
ArrayBuffer transfer
# sec-arraybuffer.prototype.transfertofixedlength

브라우저 호환성

BCD tables only load in the browser

같이 보기