Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #10466 > unrolled thread
| Started by | Archos <raul.san@sent.com> |
|---|---|
| First post | 2012-01-22 03:55 -0800 |
| Last post | 2012-01-22 13:57 +0100 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.javascript
Create array of a custom type Archos <raul.san@sent.com> - 2012-01-22 03:55 -0800
Re: Create array of a custom type Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-22 12:55 +0000
Re: Create array of a custom type Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-22 14:14 +0100
Re: Create array of a custom type Martin Honnen <mahotrash@yahoo.de> - 2012-01-22 13:57 +0100
| From | Archos <raul.san@sent.com> |
|---|---|
| Date | 2012-01-22 03:55 -0800 |
| Subject | Create array of a custom type |
| Message-ID | <4bf1eb31-c97a-472c-a7ad-a9bffa4f0db4@g41g2000yqa.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2012-01-22 12:55 +0000 |
| Message-ID | <4f1c0743$0$30171$a8266bb1@newsreader.readnews.com> |
| In reply to | #10466 |
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).
Rgds
Denis McMahon
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-01-22 14:14 +0100 |
| Message-ID | <1401647.V9SEqChMir@PointedEars.de> |
| In reply to | #10467 |
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).
[toc] | [prev] | [next] | [standalone]
| From | Martin Honnen <mahotrash@yahoo.de> |
|---|---|
| Date | 2012-01-22 13:57 +0100 |
| Message-ID | <4f1c07c2$0$6640$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #10466 |
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/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web