MENU
ArrayBuffer
'To achieve maximum flexibility and efficiency, JavaScript typed arrays split the implementation into buffers and views. A buffer (implemented by the ArrayBuffer object) is an object representing a chunk of data; it has no format to speak of and offers no mechanism for accessing its contents. To access the memory contained in a buffer, you need to use a view. A view provides a context — that is, a data type, starting offset, and number of elements — that turns the data into an actual typed array.' – MDNThe ArrayBuffer has the following properties and methods:
.length
.isView(arg)
.transfer(oldBuffer [, newByteLength])
.prototype.byteLength
.prototype.slice()
.slice()
RESETRUNFULL
<!DOCTYPE html><html><body><script>
ArrayBuffer.isView(); // false
ArrayBuffer.isView([]); // falseArrayBuffer.isView({}); // falseArrayBuffer.isView(null); // falseArrayBuffer.isView(undefined); // falseArrayBuffer.isView(new ArrayBuffer(10)); // false ArrayBuffer.isView(new Uint8Array()); // trueArrayBuffer.isView(new Float32Array()); // trueArrayBuffer.isView(new Int8Array(10).subarray(0, 3)); // truevar buffer = new ArrayBuffer(2);var dv = new DataView(buffer);ArrayBuffer.isView(dv); // true
</script></body><html>
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var buf1 = new ArrayBuffer(40);buf1.byteLength; // 40new Int32Array(buf1)[0] = 42;var buf2 = ArrayBuffer.transfer(buf1, 80);buf1.byteLength; // 0buf2.byteLength; // 80new Int32Array(buf2)[0]; // 42var buf3 = ArrayBuffer.transfer(buf2, 0);buf2.byteLength; // 0buf3.byteLength; // 0
</script></body><html>
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var buf1 = new ArrayBuffer(8);
var buf2 = buf1.slice(0);
var buf3 = buf1.slice(2,4);
console.log(buf3.byteLength); // 2
</script></body><html>