Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda