Create array of a custom type


A

Archos

I've created a custom type named person:

function person(name, age) {this.name=name; this.age=age;}

Now, how to create an array (of 10 elements) of that new type?
[10]person
 
Ad

Advertisements

M

Martin Honnen

Archos said:
I've created a custom type named person:

function person(name, age) {this.name=name; this.age=age;}

Now, how to create an array (of 10 elements) of that new type?
[10]person

var persons = [
new person("name 1", 1),
new person("name 2", 2),
new person("name 3", 3),
...
];

Arrays in Javascript don't have a particular type of elements however so
you could freely mix your type with other types in a single array.
 
Ad

Advertisements

T

Thomas 'PointedEars' Lahn

Denis said:
I've created a custom type named person:

function person(name, age) {this.name=name; this.age=age;}

Now, how to create an array (of 10 elements) of that new type?
[10]person

Not tested, but maybe you could start with something like:

var i=10;var a=new array();while(i--)a=new person("",0);

and work out why it doesn't work (because it probably won't).


Will you *please* stop wasting everybody's time by posting obviously
erroneous solutions?

Lack of code style aside, this works fine if you have that basic knowledge
that it is `new Array' in ECMAScript implementations (case-sensitive). The
easier, today irrelevantly less compatible expression is, of course, the
empty Array initializer, `[]'.

As `person' refers to a constructor, the identifier in the declaration and
call should be `Person' by good convention, of course.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top