Game Development Reference
In-Depth Information
var typedArr = new Uint32Array(3);
typedArr[] = 0; // SyntaxError
typedArr[0] = 3;
typedArr[1] = 4;
typedArr[2] = 9;
for (var i = 0, len = typedArr.length; i < len;
i++) {
typedArr[i] >= 0; // True
}
Again, just to reinforce the fact that no helper functions or shortcuts related to or-
dinary JavaScript arrays work with typed arrays, notice that an attempt to access
an element without providing an index will provide an exception being thrown by the
browser.
ArrayBuffer and ArrayBufferView
Although, all the previous examples used a specific kind of typed array directly, the
way that typed arrays work is slightly more involved than that. The implementation
is broken down into two separate parts, namely, an array buffer and a view (or more
specifically, an array buffer view). The array buffer is simply a chunk of memory that
is allocated, so we can store our data there. The thing about this buffer is that it has
no type associated with it, so we can't access that memory to store data to, or read
data from it.
In order to be able to use the memory space allocated by the array buffer, we need a
view. Although the base type for this view is ArrayBufferView , we actually need
a subclass of ArrayBufferView , which defines a specific type to the data stored
in the array buffer.
var buffer = new ArrayBuffer(32);
buffer.byteLengh == 32; // True
Search WWH ::




Custom Search