ArrayBuffer

Find out what ArrayBuffer is and how to use it

Like aspotIs an opaque representation of the data available on the disk,ArrayBufferIs an opaque representation of the bytes available in memory.

The constructor takes one parameter, and the length is in bytes:

const buffer = new ArrayBuffer(64)

The ArrayBuffer value has a (read-only) property:byteLength, As the name implies, its length is expressed in bytes.

It also providesslice()Create new instance methodArrayBufferStarting from the existing position, based on the starting position and optional length:

const buffer = new ArrayBuffer(64)
const newBuffer = buffer.slice(32, 8)

Download data from the Internet as an ArrayBuffer

We can download a Blob from the Internet and store it in ArrayBuffer, usingXHR:

const downloadBlob = (url, callback) => {
	const xhr = new XMLHttpRequest()
	xhr.open('GET', url)
	xhr.responseType = 'arraybuffer'
<span style="color:#a6e22e">xhr</span>.<span style="color:#a6e22e">onload</span> <span style="color:#f92672">=</span> () =&gt; {
<span style="color:#a6e22e">callback</span>(<span style="color:#a6e22e">xhr</span>.<span style="color:#a6e22e">response</span>)
}

<span style="color:#a6e22e">xhr</span>.<span style="color:#a6e22e">send</span>(<span style="color:#66d9ef">null</span>)

}

Download mine for freeJavaScript beginner's manual


More browser tutorials: