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

373
Views
When should I use functions as classes in Javascript?

I wish to create two sets of information that I'm going to re-use, one for a beer and one for a dispenser of beer, in Javascript.

The information is retrieved from an API, and all the beers are objects with an ID, name etc.

I'm wondering if I should bother with making each beer into a function like this:

function Beer() {
   this.Id = 0;
   this.Name = "";
   
   etc.
}

and then instantiate by setting a variable to a new Beer() through a forEach loop on the parsed JSON string. Is it worth it? Or should I just take the information directly from the API every time I reference different information about each beer? In other words, if this is not the time to use functions as pseudo-classes, when is?

If it helps, I'm writing in MVC, so I'm planning on having the information about the beer and the dispenser in my model, so I can use it with the controller and display it in my view very easily.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

For performance, function calls in JavaScript are as cheap as a just set of statements. So it won't affect performance significantly.

For simplicy, if all you need from function Beer is to copy properties from a JSON, without adding methods. That's simply not necessary. A more convenient way for parsing/normalizing objects from JSON is just apply a mapping function, like:

let objArray = jsonArray.map(json => ({
    Id: json.Id || 0,
    Name: json.Name
}))
about 4 years ago · Santiago Trujillo 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!