By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

how subclass native class?

P: n/a
How can I subclass a native class to get my own class with all the
function of the native clas, plus correct instanceof behaviour?

The scenario I'm thinking of is something like

function MyArray() {
Array.apply( this, arguments );
}
MyArray.prototype = new Array();
MyArray.prototype.constructor = MyArray;

var marr = new MyArray();
marr[0] = "abc";
marr.length == 1; // true
marr instanceof MyArray; // true

But this seems impossible due to the Array constructor returning a new
object even when called with my own "this" in the apply() above. Can
this be solved in any way?

Best regards
Mike

Sep 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
mi*****@hotmail.com wrote:
How can I subclass a native class to get my own
class with all the function of the native clas,
plus correct instanceof behaviour?

The scenario I'm thinking of is something like

function MyArray() {
Array.apply( this, arguments );
}
MyArray.prototype = new Array();
MyArray.prototype.constructor = MyArray;

var marr = new MyArray();
marr[0] = "abc";
marr.length == 1; // true
marr instanceof MyArray; // true

But this seems impossible due to the Array constructor
returning a new object even when called with my own "this"
in the apply() above. Can this be solved in any way?
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).

Array objects may be modified by augmentation, or the array prototype
extended.

Richard.
Sep 10 '06 #2

P: n/a
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 ;-).
Array objects may be modified by augmentation, or the array prototype
extended.
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.

BTW: Does this subclassing limitation apply to all native classes
(Date, Number, ...) ? 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?

Best regards
Mike Wilson

Sep 10 '06 #3

P: n/a
mi*****@hotmail.com wrote:
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).
>Array objects may be modified by augmentation, or the
array prototype extended.

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.
Sep 10 '06 #4

P: n/a
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.
Yes, the duck typing thing would normally be ok. My aim with the array
subclasses in this case was to map typed Java lists on the server to
corresponding array classes in JS (I am already doing this with
objects).

F ex the class Person with the member "List<Addressaddresses" would
be mapped to the corresponding JavaScript class Person (subclass to
Object) with a property "addresses" of class AddressArray (subclass to
Array).

So the whole thing with subclasses and instanceof is really to make it
easier for a programmer fluent with the server model, trying to map
everything as closely as possible. So the classes are a sort of "sugar"
to make the feel of the client's server API better.
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
Thanks, I had a look through it and yes, I will probably need to invest
some time to learn how to use it :-).

Best regards
Mike

Sep 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.