Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda