EMI
EMI

Reputation: 105

Javascript Class Definition with Array Member

I wrote the code in Java, no problem. Here I am giving the Java code:

public class CarGallery {

    static int carCounter=10;
    static Gallery[] car = new Gallery[carCounter]; 

    public static void main(String[] args) {
    car[0].weight = (float) 1.25;
    car[0].weight = (float) 0.87;
    // ... and so on ... // 
    }   
}

 class Gallery {

    public float weight;
    public float height;
    public int colorCode;
    public int stockGallery;
};

The thing is, I want to write the same code in Javascript. Here is the code that does not work:

var cars = {weight:0 , height:0 , stock:0 , model:"..."};
var cars = new Array();

cars[0].weight=1.2;
cars[0].height=0.87;
cars[0].stock=2;
cars[0].model="320";

Thank you for your help!

Upvotes: 0

Views: 72

Answers (1)

jordanwillis
jordanwillis

Reputation: 10705

The JavaScript equivalent to your Java would be.

function Gallery() {
  this.weight = null;
  this.height = null;
  this.colorCode = null;
  this.stockGallery = null;
};

var carCounter = 10;
var carGallery = new Array(carCounter);

carGallery[0] = new Gallery();
carGallery[0].weight = 1.2;
carGallery[0].height = 0.87;
carGallery[0].colorCode = 2
carGallery[0].stockGallery = 3;

carGallery[1] = new Gallery();
carGallery[1].weight = 2.2;
...

Upvotes: 1

Related Questions