Non-extensibility of Typed Arrays

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Aug 27 16:14:29 PDT 2013


On Aug 27, 2013, at 3:49 PM, David Herman wrote:

> On Aug 27, 2013, at 9:47 AM, Filip Pizlo <fpizlo at apple.com> wrote:
> 
>> I do. Placing named properties on arrays makes sense. Consider a matrix implemented as a Float32Array, with named properties telling you the numRows and numCols. Just one example. 
> 
> There are of course other ways to achieve this that don't involve patching the array object, such as building a data abstraction for matrices that has-a Float32Array, or creating a new array type with additional methods:
> 
>    var Matrix = new ArrayType(float32);
>    Matrix.prototype.numRows = function() { ... }
>    // or
>    Object.defineProperty(Matrix.prototype, { get: function() { ... }, ... });


or even better:

  class Matrix extends Float32Array {
          get numRows() {...}
          ...
   }

although "Matrix" may be a bad example...

Subclasses of Typed Arrays get their own prototype that can add or over-ride inherited methods.  The instances of the subclass are still non-extensible according to the current spec. draft.

Allen





More information about the es-discuss mailing list