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

124
Views
Why can't I call a static method from the constructor?

I'm getting into ES6 and trying to turn a class that's over 2700 lines long into a collection of small, easy-to-read files.

I'd like to move some initialization code from the constructor into their own methods. But I'm a bit stuck. Consider this code:

export default class Thingy {
  non_static_prop;

  initNonStaticStuff() {
  }

  constructor(args) {
    this.non_static_prop = 'some non-static prop set by the constructor'; // ok
    console.log(this.non_static_prop); // works as expected
    // now can we get a method to do the same thing?
    initNonStaticStuff(); // fails:
      /**
       * `Uncaught ReferenceError: initNonStaticStuff is not defined`
       */
    // So I guess we'll have to initialize everything here.
    // But then how am I supposed to make my code smaller and more modular?
  }
}

I'm confused. What options do I have to run code from the constructor?

I could, of course, call a static method instead, but then this wouldn't let me access non-static properties.

Another option I see is to ask the page that creates the instance to then run initNonStaticStuff() (e.g. say by way of a start() method), but I'd rather skip such a step if possible.

What did I miss?

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

0

Just call it with this.initNonStaticStuff();

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!