0

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 ?

3
  • JavaScript is a Prototype based object model, not class based. Commented Sep 19, 2011 at 3:48
  • @Canesin the answers in this question can be very clarifying. Commented Sep 19, 2011 at 4:18
  • This question is ancient, but Arrays and TypedArrays are very distinct types. The links at the bottom of the question are about Arrays and aren't very relevant. Commented May 15, 2022 at 19:01

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It appears you can make some progress using ES6 proxies, but performance drops significantly.
@CMCDragonkai class SubArray extends Array { randomElement() { return this[Math.floor(Math.random() * this.length)] } } works for me.
@rfl890 yea the answer is from 2011 :)
Notice the question also is specifically asking about typed arrays, not Array. IIRC they were only introduced in ES6 (which was a big buzzword back then, and still a few years out in 2011), together with the possibility to subclass builtins.
Notice the question also is specifically asking about typed arrays, not Array. IIRC they were only introduced in ES6 (which was a big buzzword back then, and still a few years out in 2011), together with the possibility to subclass builtins.
2

It's possible to extends TypedArrays with class syntax.

class Vector extends Float64Array {
  constructor(i=0, j=0) {
    super([i, j])
  }
}
> new Vector()
Vector(2) [Float64Array] [0, 0]

Relevant part of the spec:

23.2.5 The TypedArray Constructors Each TypedArray constructor:

  • ...
  • may be used as the value of an extends clause of a class definition. Subclass constructors that intend to inherit the specified TypedArray behaviour must include a super call to the TypedArray constructor to create and initialize the subclass instance with the internal state necessary to support the %TypedArray%.prototype built-in methods.

Comments

1

As others have said: Alas, you can't subclass arrays (or typed arrays) in any meaningful way. But you can create an alias, e.g.

global.Vector = Float64Array

And you can add additional methods to the Float64Array prototype, e.g.

Float64Array::last = -> @[@length - 1]

It's not ideal from a modularity standpoint, but for a self-contained app, it's nice to be able to add that sort of syntactic sugar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.