Non-extensibility of Typed Arrays

Oliver Hunt oliver at apple.com
Tue Aug 27 16:30:39 PDT 2013


On Aug 27, 2013, at 3:49 PM, David Herman <dherman at mozilla.com> 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() { ... }, ... });

So what is the answer for jQuery like libraries that want to be able to add metadata?

It's possible (if you want) to preventExtensions() on any type, but you can't undo it.

> 
>>> TA instances having no indexed expandos but allowing named ones is weird. Better to be consistent to users
>> 
>> Consistency would imply doing what other indexed types do. 
> 
> Consistency arguments won't get you very far. The indexed properties of typed arrays by design act very differently from other indexed types. That's their whole reason for existence.
> 
> And the other consistency dimension is between array types and struct types. Is anyone arguing that structs should also have expandos?

No, but i would expect expandos to be possible on an Array of them.  The same argument being made in favor of preventExtensions() on TAs applies to all new types in ES6 -- why should i be able to add expandos to a Map or any other type?  (Map is particularly severe given the overloaded nature of [] in other languages and often "correctish" enough behavior of toString() in ES, e.g. m=new Map; m[someInt]=foo; … m[someInt])

--Oliver

> 
> Dave
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list