Skip to content
Advertisement

extend Uint8Array then map gives wrong result

Why does hex() implementation do not work as expected but the hex2() works fine.

JavaScript

Advertisement

Answer

All “legit” .map methods will return an object of the same interface than the one on which it exists.

So on a TypedArray, it will return a TypedArray.

JavaScript

But a TypedArray can’t hold Strings, only numbers, and for the Uint8Array Typed Array, only integer numbers in the range 0 to 255.
So Uint8Array.map( fnThatReturnsStrings ) will try to cast all the return values to numbers in that range.

"01" => 1
"02" => 2
"1e" => NaN => 0

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement