How do I subclass/extend TypedArrays in JavaScript?

I'm writing an library in CoffeScript (so JS), and it is heavy math.. I really need to work with typed arrays (Float64Array) and all the performance they offer.

So what is the best way to extend the typed array functionality ??

Currently I'm doing it like functions:

Vector =
    create: (ag...) ->
        CGE2Point.create ag...
    dot: (i,j) ->
        i[0]*j[0] + i[1]*j[1]
    add: (i,j) ->
        @.create i[0]+j[0], i[1]+j[1]
    sub: (i,j) ->
        @.create i[0]-j[0], i[1]-j[1]
    mul: (s,v) ->
        @.create s * v[0], s * v[1]
    div: (s,v) ->
        @.create v[0] / s, v[1] / s

But would be really nice to have a Vector object, that inherits from the typed array. I know that the approach:

class Vector extends Float64Array

Create a class that do not have the full benefits the typed array Question about subclassing array , reading the following articles Dean Edwards suggests getting an object copy from an iframe, this other reference do it in other way Sorry Dean. But typed array do not have all that methods.

So, how is the right (or at least most elegant and performing) way to subclass the typed arrays ?? Or should I write all like functions ?


  • Subclassing arrays in JavaScript is not really possible. Kangax (of Prototype fame) has written up a thorough elaboration of how/why it just doesn't work out.

    edit — this answer is old. Modern JavaScript does provide the ability to subclass the built-in Array class properly.