1

as we know we can declare an array like this

for (int i=0;i<array.length;i++)

{ d[i]=new array();}

What about an object I want to declare more than 10 objects and I think it's not efficient to write a declare statements for 10 times !!like this

car c1 = new car();
car c2= new Car(); 

..etc

what can I do ?

Sara Leza
  • 11
  • 2
  • 3
    Javascript doesn't have explicit data types... – millimoose Oct 12 '11 at 14:03
  • 1
    I'm assuming your `car c1` and `car c2` were meant to be `var c1` and `var c2` – vol7ron Oct 12 '11 at 14:06
  • 1
    Also I'm assuming that with `new car()` and `new Car()` you meant `new Car()` for both, JavaScript is case sensitive. – stivlo Oct 12 '11 at 14:11
  • Also, I'm assuming by `int i` you meant `var i` – vol7ron Oct 12 '11 at 14:17
  • Mmmm I want to ask car seller to insert Car Info. then save it in a linkedlist this linkedlist's elements are objects from Car class So can I declare more than 1 object according to seller numbers of cars !!? – Sara Leza Oct 12 '11 at 14:23

4 Answers4

6

You are mixing the things a little.

var array = [];

for (var i = 0; i < 10; i++)
{
    array[i] = new Car();
}

As Daniel noted, you can even use Array.push() in this way:

var array = [];

for (var i = 0; i < 10; i++)
{
    array.push(new Car());
}

The point is that you declare the array with var array = [] (or with var array = new Array(), see the differences here What’s the difference between "Array()" and "[]" while declaring a JavaScript array?) and you set the items at the index you want (in Javascript arrays are dynamicly sized)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • +1. If you are accumulating a collection of cars like this, it's better to use an array than 10 independent variables. – Jon Gauthier Oct 12 '11 at 14:05
  • @HansEngel It depends on the language though. I once heared from a flash developer that in Flash writing 1 statement 10 times is faster then using a loop. – Tom Dezentje Oct 12 '11 at 14:07
  • `int i`? Wasn't aware Javascript had `int` – vol7ron Oct 12 '11 at 14:12
  • @vol7ron Int? you see ints? I don't see ints :-) – xanatos Oct 12 '11 at 14:14
  • @vol7ron No C#, and every time I write JS I have to correct all my `int` and `string` :-) At least the `{}` and the `[]` gets correctly. – xanatos Oct 12 '11 at 14:16
  • @vol7ron And I'll add that the proposal for [ECMAScript 4.0](http://www.ecmascript.org/es4/spec/overview.pdf) HAD strongly typed variables. But it was like `var p : int`. – xanatos Oct 12 '11 at 14:20
3

Use an array of objects like:

var cars = [];
for (var i = 0; i < 10; i++) {
    cars.push(new Car());
}
millimoose
  • 39,073
  • 9
  • 82
  • 134
2

Hum, what you're doing in your first example is declaring an array of array. To create an array, simply do

var a = [];

To create and maintain many objects, put them in that array:

for(var i = 0; i < 10; i++)
    a[i] = new Car();

car[0].drive(); //Drive first car
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
0

You can:

  1. use eval (it was designed for things like this)
  2. use an array of objects

  1. for (var i=1,n=3; i<n; i++)
       eval("c" + i + "= new Car()");
    
  2. var a = [];
    for (var i=1,n=3; i<n; i++)
       a[i] = new Car();
    
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • 2
    How was `eval` designed for things like this? – Alex Turpin Oct 12 '11 at 14:05
  • @Xeon06: eval was created to run dynamically generated code, which should not depend on the client input/values. If you want a particular set of variables, that follow a name scheme, but want to condense your code, you'd use `eval` (See the example). – vol7ron Oct 12 '11 at 14:07
  • Well I'd still rather use an array for numbered variable names like that, but I can see where this might be useful. – Alex Turpin Oct 12 '11 at 14:29
  • Me too, but this wasn't about our choice, I was answering the OPs question :) – vol7ron Oct 12 '11 at 15:06