Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Create array of a custom type

Reply
Thread Tools

Create array of a custom type

 
 
Archos
Guest
Posts: n/a
 
      01-22-2012
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-22-2012
Archos wrote:
> 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.


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-22-2012
Denis McMahon wrote:

> On Sun, 22 Jan 2012 03:55:03 -0800, Archos wrote:
>> 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[i]=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
--
When all you know is jQuery, every problem looks $(olvable).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom allocation and custom type castironpi Python 0 08-04-2008 07:15 PM
Error Consuming Web Service from WIndows application when WebService is using Custom Service Account ( Create an Application Pool with a Custom Identity) DNB ASP .Net Security 1 01-22-2008 09:08 PM
Error Consuming Web Service from WIndows application when WebService is using Custom Service Account ( Create an Application Pool with a Custom Identity) DNB ASP .Net Web Services 1 01-20-2008 01:47 PM
Maven using ANT plugin for SCP task : Embedded error: Could not create task or type of type: scp. krabhi Java 1 08-09-2006 04:19 PM
Question: Invoking custom file type (file associated with custom app) VB Programmer ASP .Net 1 11-03-2003 11:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57