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

210
Views
Calling static method from different class doesn't work without using default

I've just finished a lengthy debugging session and while I solved the issue, I have no idea why my solution is necessary.

I have a file Update.js that looks like this:

class Update {

  static doThisAfterClick = (event) => {
    console.log("First button clicked!");
  }

  static doThisWhenCalled = (name, age) => {
    console.log("My name is " + name + " and my age is " + age);
  }
}

export default Update;

In Edit.js I import the module:

let Update = require('./Update');

class Edit {

  static fancyEventHandler = (event) => {
    Update.doThisWhenCalled("Peter", 42);
  }  

}
export default Edit;

and later, in app.js I add a few Event handlers:

import Edit from "./Edit";
import Update from "./Update";

firstButton.addEventListener('click', Update.doThisAfterClick);

secondButton.addEventListener('click', Edit.fancyEventHandler); 

Now the crazy part: Clicking firstButton logged First button clicked! as expected, but clicking secondButton gave an error: Uncaught TypeError: Update.doThisWhenCalled is not a function..

I actually tried to debug this by inserting

let x = Update.doThisWhenCalled;
debugger;

into fancyEventHandler() and saw that x was undefined.

Through some sheer luck I saw that I could use

Update.default.doThisWhenCalled("Peter", 42);

instead and this resolved the issue. No idea why.

What is wrong with calling the static method directly? And what is this "default" property?

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

0

I think it's related to the way you're exporting/importing, below is a possible fix:

Update.js:

class Update {
  static doThisAfterClick() {
    console.log("First button clicked!");
  }
  static doThisWhenCalled(name, age) {
    console.log("My name is " + name + " and my age is " + age);
  }
}

export default Update;

Edit.js:

import Update from "./Update";

class Edit {
  static fancyEventHandler() {
    Update.doThisWhenCalled("Peter", 42);
  }
}

export default Edit;
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!