0

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";
  • I read some documents and saw that there is no class definition in Javascript like Java classes.
  • I found class definition in JS with constructors, but I don't want to use constructors.
  • The member should be defined as array like:

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

Thank you for your help!

1 Answer 1

1

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;
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you jordanwillis, it works without any problem! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.