Game Development Reference
In-Depth Information
32 bits number. The integer argument that we pass in, indicates the size of the
array. Once this array is created, its size cannot be changed. Any values assigned to
it outside its bounds are silently ignored by the browser, as well as any illegal values.
Other than the restrictions on what can be stored in this special array, it all may seem
just as an ordinary JavaScript array to the untrained eye. But if we look a bit deeper
into it, we'll notice a couple more distinctions between an array and a typed array.
typedArr instanceof Int32Array; // True
typedArr.length == 10; // True
typedArr.push(23); // TypeError: <Int32Array>
has no method 'push'
typedArr.pop(); // TypeError: <Int32Array> has
no method 'pop'
typedArr.sort(); // TypeError; <Int32Array> has
no method 'sort'
typedArr.buffer instanceof ArrayBuffer; // True
typedArr.buffer.byteLength == 40; //True
typedArr instanceof Array; // False
The first thing we notice is that the array is indeed an Int32Array , and not an
Array. Next, we're happy to know that the length property is still there. So far so
good. Then, things start to separate, as simple methods associated with regular ar-
rays are no longer present. Not only that, but there's also a new attribute in the
typed array object named buffer . This buffer object is of type ArrayBuffer ,
which has a byteLength property. In this case, we can see that the buffer's
length is 40 . It's easy to see where this 40 came from: buffer holds 10 elements
( typedArr.length ), and each element is 32 bits long (4 bytes), for a total of 40
bytes in the ArrayBuffer (hence the property name of byteLength ).
Since typed arrays don't come with helper functions such as regular JavaScript ar-
rays do, we read and write data to them using the old array notation, where we index
into the array in order to read or write a value.
Search WWH ::




Custom Search