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

121
Views
Having variables in the constructor vs in the actual class?

The first of the below code examples is what I am doing today, where I initialise the global variables in the constructor.

But would there be any harm in moving the global variables out in the class, as seen in the second example?

class Alert {
  constructor(alert) {
    this.load = JSON.parse('{}');
    this.alert = alert;
    this.#coolDownTime = 0;
  };

  #coolDownTime;
}

vs just doing

class Alert {
  constructor(alert) {
    this.alert = alert;
  };

  #coolDownTime = 0;
  #load = JSON.parse('{}');

}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

JavaScript classes are functions, which are objects. (POOP (prototype object-oriented programming)).

Here's an interesting concept, since JavaScript classes are functions, then what do we actually write? JavaScript ES6 classes are just syntactic sugar for prototypes. This means, that both of your ideas are actually the same.
This is exactly how creating a method works.

Here is a good reference: Are ES6 classes just syntactic sugar for the prototypal pattern in Javascript?

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!