Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
How to work with classes in javascript, as well as with a collection of objects of these classes?

Good afternoon. In C#, I create a class, then from this class I create a class variable (or object). And if I work with collections, then I add my new classes to the collection.

class person{
   int id;
   string name;
   int age;
}
person myPerson = new person();
List<person> people = new List<person>();
people.add(myPerson);

As I understand it, in Javascript it happens in a different way. Here they do not like to create classes first, but directly add an object with data directly to the collection.

Tell me how Javascript is used to work with its own data types. What is the approach used?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can work in JS (since ES6) exactly as in C# :

C# code :

class person{
   int id;
   string name;
   int age;
}
person myPerson = new person();
List<person> people = new List<person>();
people.add(myPerson);

JS code :

class Person
{
    constructor(id, name, age)
    {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

myPerson = new Person("p1", "Tom", 54); 
people = [];
people.push(myPerson);

Hope it helps ! :)

about 4 years ago · Juan Pablo Isaza Report

0

C# is a strongly typed language, that is, you can declare a variable to be an instance of an object (or a primitive type) and then the language will throw an error if you violate this. So, a collection like this

List<person> people = new List<person>();

makes sense in C#. However, Javascript is a weakly typed language, that is, you can define a variable to be an instance of a class, but you cannot declare it. So, if you do something like this:

var myVariable = new MyClass();

then you have instantiated MyClass in your definition, but you only declared myVariable to be a variable.

In Javascript you can use arrays or objects as collections. Example using array:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

var myCollection = []; //creating the collection
myCollection.push(new Rectangle(2, 3)); //adding an element to a collection
myCollection.push(new Rectangle(3, 2)); //adding an element to a collection
myCollection.push(new Rectangle(4, 4)); //adding an element to a collection
myCollection.push(new Rectangle(10, 15)); //adding an element to a collection

for (let rectangle of myCollection) {
    console.log(`${rectangle.width} x ${rectangle.height}`);
}

Example using object

let myCollection = {
    'first': 'a',
    'second': 'b',
    'third': 'c'
}

myCollection.fourth = 'd';

for (let key in myCollection) console.log(`${key} is ${myCollection[key]}`);

In Javascript, class is a syntactic sugar for functions. Example:

function MyClass(foo, bar) {
    this.myFoo = foo;
    this.myBar = bar;
}

let obj = new MyClass('abc', 'def');

console.log(obj);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!