Thanks Richard,
You could not subclass an array and have a result that
had the characteristics of an array (specifically a
self-adjusting/updating - length - property) because the
characteristic that makes an Array and Array is internal
to the array instance (its internal [[Put]] method).
Well, then I know that it is indeed not possible. Thanks
anyway for bringing the bad news ;-).
At least I now know you did want the 'magic' length property rather than
just to inherit Array methods. Inheriting just array methods doesn't
work either, but that is because of a number of varying implementation
bugs in various language versions. Array.prototype.join is virtually the
only one that can be reliably used on an object that has an array as its
prototype (which is actually quite useful).
Ok, I will have to consider these solutions instead.
I was hoping to be able to use instanceof for
distinguishing between "plain" arrays and my own, but
this seems to be out of the question.
That makes it sound like augmentation will be your preferred option. But
that doesn't make using - instanceof - practical, though in all the
browser scripts I have ever written I have never had a reason to use -
instanceof -, and don't ever anticipate having to. It is very easy to
provide objects with a testable indicator of whatever you need to know
about them (if their normal properties are not sufficient in
themselves), and most of the time it is not necessary to care.
BTW: Does this subclassing limitation apply to all native
classes (Date, Number, ...) ?
More or less. Date, String, Number, Boolean and RegEx all use an
internal [[Value]] property that is inaccessible and cannot be
inherited, and an object inheriting from a function will not have
[[Call]] or [[Construct]] methods. Some methods of these objects are
specified as being applicable to other types of objects, but some bugs
exist in this area so outside a known environment it is not worth the
effort.
My own quick tests suggest it is so...
I've skimmed through a couple of JS books but didn't find
this kind of information. Do you have any good pointers to
where to look in cases like this?
The specification (ECMA 262, 3rd edition) is where you look for the
details of the language. It is not an easy read but will tell you what
to expect (and then experimentation will show you that what you should
expect mostly happens with a very few exceptions).
Richard.