I misunderstood the problem: I thought there was something peculiar about an
array instance (the first object in the prototype chain), but it’s about the
Array constructor not being invocable as a function (right?).
Wouldn’t it be easier to introduce a generic method Array.prototype.init() that
behaves like quirk-free Array.prototype.constructor()?
Use case 1: Create an array subtype:
let SubArray = Array <| function(...args) {
super.init(...args); // instead of super.constructor(...args)
}
Use case 2: Avoid the length pitfall:
var arr = new Array().init(5); // same as [5]
- Another, possibly stupid, idea: Could Array.prototype.constructor not point
to Array, but to a function like Array.prototype.init() – that doesn’t create a
new instance and doesn’t have the length quirk?
- One more: Introduce a constant CLEAN
let SubArray = Array <| function(...args) {
super.constructor(CLEAN, ...args);
}
var arr = new Array(CLEAN, 3); // the same as [3]
- Creating a subtype CleanArray or ExtensibleArray also seems feasible. Engines
could optimize internally so that it’s basically the constructor being swapped
for a quirk-free implementation.
Whatever the solution, it’s obviously applicable to Date, Error, etc., as well.
> In Smalltalk the default implementation of species is:
> species
> ^self class
>
> which is pretty much the moral equivalent of: this.constructor.
>
> However, that might introduce backwards compatibility issues for existing
> code which has constructors but currently always produces Array instances
> from these functions.
The problem are Array subtypes that expect slice() et al. to produce instances
of Array (and not of themselves) (?)
> function createArraySubclass(proto,...values) {
> return proto <| [...values].{
> [Array.derivedArrayKey](){return proto<| [ ]}
> }
> }
@derivedArrayKey would normally be in the prototype, right?
--
Dr. Axel Rauschmayer
[email protected]
home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss