Defining a Class in Javascript with an Array Property

After successfully writing the code in Java, I am now attempting to write it in JavaScript. Here is the current 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;
};

Below is my attempt at replicating the Java code in JavaScript, which currently 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";
  • Upon further research, I discovered that there isn't a direct equivalent of classes in JavaScript like there is in Java.
  • In JavaScript, class-like behavior can be achieved using constructors, however, I prefer not to use constructors.
  • The member should be defined as an array as seen here in the Java code:

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

Any assistance or guidance you can provide would be greatly appreciated!

Answer №1

Your JavaScript code can be translated from Java as follows:

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

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

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

carList[1] = new Car();
carList[1].weight = 2.2;
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering the country associated with a country code using ngx-intl-tel-input

In my application, I am trying to implement a phone number field using this StackBlitz link. However, I have observed that it is not possible to search for a country by typing the country code (e.g., +231) in the country search dropdown. When I type a coun ...

Prevent my application from being idle (Cordova/Android)

I have recently developed an Android app using a node.js webpack project. After installing the app on my phone, I observed that it enters a sleep mode along with the phone. Consequently, a JavaScript timer I set up stops functioning as intended: pingTime ...

Transforming dynamic values within a PHP array into static keys

Query: I currently have an array in PHP structured as follows Array ( [read] => 0 [edit_posts] => 1 [delete_posts] => 2 ) I need to update the values 0, 1, 2 to all be 1, like so Array ( [read] => 1 [edit_posts] => 1 ...

Utilize the pixels from a live video feed of a webcam through JavaScript, WebRTC, and MediaStreamTrack

My scientific project requires real-time processing of video streams from a webcam using JavaScript. Thanks to WebRTC, displaying live video on a website is easy, and <canvas> allows for capturing and processing screenshots. You can learn more about ...

Utilize React and Django to showcase encoded video frames in your application

Having recently ventured into the world of web development, I've been facing a challenging problem that I can't seem to crack. My tech stack involves the use of React and Django. The issue at hand is with a 3rd party application that utilizes op ...

Nock is capturing my request, however, my AJAX call is encountering an error

I am currently conducting a test on an AJAX request using the XMLHttpRequest method: export default function performTestRequest() { const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://example.com/service'); xhr.onload = ( ...

Ways to halt a watch statement or digest cycle within Angular

At the moment, I have implemented a text input linked to a model using a $scope.watch statement to observe changes in the model. The purpose of this setup is to create an auto-complete / typeahead feature. <!-- HTML --> <input type="text" ng-mode ...

What are some ways to access the most updated status in redux once the thunks and actions are complete?

After the thunks and actions have completed, how can I access the current state of redux? The issue arises in the handleSubmit function where if there are errors while registering a user, the redux status gets updated with the message "Email already regist ...

How to dynamically modify ion-list elements with Ionic on button click

Imagine having 3 different lists: List 1: bus, plane List 2: [related to bus] slow, can't fly List 3: [related to plane] fast, can fly In my Ionic Angular project, I have successfully implemented the first ion-list. How can I dynamically change th ...

Discovering objects generated by Hibernate: Techniques for identification

In our current project, we are utilizing hibernate3 and spring 3. We have a large number of domain objects with some eager relations between them. I am working on optimizing the application by creating an eager-fetch diagram between these objects. However ...

Is the count property of an Array considered to be optional?

I defined a type alias like this: typealias subjectsListType = Array<Dictionary<String, String>> I then declared an optional variable: var subjectsList : subjectsListType? = nil In order to display the number of elements in the array in a tab ...

Safari compatible JavaScript needed for enforcing required fields in HTML5

I have been using the following script to adjust the HTML5 required attribute of my input elements. However, I am now looking for a way to tweak this script so that it can also function properly in Safari browsers, where support for this attribute may be l ...

Creating a Custom Error Page in SpringBoot

After developing an application with SpringBoot that features webservices and a front-office coded in ReactJs, the default port was set to 8080. To simplify the URL access, I decided to switch this application to port 80 by adding the code snippet below to ...

Decoding JSON information variables

Exploring JSON responses in Android Studio has been an interesting journey for me. I've been working on understanding the example provided in this tutorial. Currently, I have managed to display the 'id' and 'content' sections of a ...

Challenge with setting a variable during the linking phase in Makefile

I have a question regarding defining a variable HASH_TABLE_SIZE at link time and including it in my makefile. This is the current setup of my Makefile: 1 CC = g++ 2 CFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic -D HASH_TABLE_SIZE=10 3 LDFLAG ...

Failure to load a picture does not trigger onError in the onLoad function

Is there a way to ensure that an image always loads, even if the initial load fails? I have tried using the following code snippet: <img class="small" src="VTVFile4.jpg" onload="this.onload=null; this.src='VTVFile4.jpg?' + new Date();" alt=" ...

Retrieve the value from Field using Field.get() and return null if the value is not found

Hey there, I've been trying to use annotations to register settings for a Feature, but I'm having trouble reflecting the setting field. Can anyone help me figure out what I did wrong? Thanks! Here's my Feature initialization where I want to ...

Discovering every possible combination of strings within a JavaScript array

Is there a way to iterate through an array of string values and generate all possible combinations for N number of strings? I also need to save each combination so I can use them to create database records later on. I came across a combination generator ...

The expiration time and date for Express Session are being inaccurately configured

I'm experiencing an issue with my express session configuration. I have set the maxAge to be 1 hour from the current time. app.use( session({ secret: 'ASecretValue', saveUninitialized: false, resave: false, cookie: { secure ...

The request made to `http://localhost:3000/auth/signin` returned a 404 error, indicating that

My goal is to access the signin.js file using the path http://localhost:3000/auth/signin Below is my code from [...nextauth].js file: import NextAuth from "next-auth" import Provider from "next-auth/providers/google" export default N ...