Blobs and Buffers Flashcards
What does a blob consist of in JS?
Blob consists of an optional string type (a MIME-type usually), plus blobParts.
What is the constructor to create a new Blob?
new Blob(blobParts, options);
What should the blobParts argument be in this constructor
new Blob(blobParts, options);
blobParts is an array of Blob/BufferSource/String values.
What should options be in this constructor
new Blob(blobParts, options);
an optional object with properties, type and endings:
- type – Blob type, usually MIME-type, e.g. image/png,
- endings – whether to transform end-of-line to make the Blob correspond to current OS newlines (\r\n or \n). By default “transparent” (do nothing), but also can be “native” (transform).
What is an ArrayBuffer?
The basic binary object is ArrayBuffer – a reference to a fixed-length contiguous memory area.
What does the following code do?
let buffer = new ArrayBuffer(16); // create a buffer of length 16
alert(buffer.byteLength); // 16
This allocates a contiguous memory area of 16 bytes and pre-fills it with zeroes.
ArrayBuffer is not an array of something, give 3 reasons why?
It has a fixed length, we can’t increase or decrease it.
It takes exactly that much space in the memory.
To access individual bytes, another “view” object is needed, not buffer[index].
How do you manipulate an array buffer?
To manipulate an ArrayBuffer, we need to use a “view” object.
What does a view object store?
A view object does not store anything on its own. It’s the “eyeglasses” that give an interpretation of the bytes stored in the ArrayBuffer.
How does the Uint8Array view the bytes in an array buffer?
Uint8Array – treats each byte in ArrayBuffer as a separate number, with possible values from 0 to 255 (a byte is 8-bit, so it can hold only that much). Such value is called a “8-bit unsigned integer”.
How does the Uint16Array view the bytes in an array buffer?
Uint16Array – treats every 2 bytes as an integer, with possible values from 0 to 65535. That’s called a “16-bit unsigned integer”.
What is the common term for all these views (Uint8Array, Uint32Array, etc)?
Typed Array is the common term for all these views (Uint8Array, Uint32Array, etc). They share the same set of methods and properties.
ArrayBuffer is a memory area. (repeat this to memorize) What’s stored in it?
a raw sequence of bytes.
Is there a TypedArray constructor?
There is no constructor called TypedArray, it’s just a common “umbrella” term to represent one of the views over ArrayBuffer: Int8Array, Uint8Array and so on,
Do Typed arrays behave like regular arrays?
Yes, Typed arrays behave like regular arrays: they have indexes and are iterable.