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

193
Vistas
JavaScript: How to initialise a class object with the new keyword in a mapper outside the class?

I have a simple console application with some tests. All I am testing is to check that the game starts with level 1. If the current level is 2 the next one should be 3 etc. The following code works fine but I wanted to move out the gameMapper function to its own file and import it within the Game.js.

Game.js

export default class Game {
  constructor(level) {
    this.level = level;
  }

  static start = () => new Game(1);

  getNextLevel = () => gameMapper.find((g) => g.level === this.level).nextLevel;
}

const gameMapper = [
  { level: 1, nextLevel: new Game(2) },
  { level: 2, nextLevel: new Game(3) },
  { level: 3, nextLevel: new Game(4) },
];

sample.test.js

import Game from '../src/Game';

describe('Test Game levels', () => {
  test('Game should start from level 1', () => {
    const game = Game.start();
    expect(game.level).toBe(1);
  });

  test('When game level is 2, the next level should be 3', () => {
    const game = Game.start().getNextLevel().getNextLevel();
    expect(game.level).toBe(3);
  });
});

The above code works completely fine. I have tried the following solutions but they did not seem to work for me. If anyone knows any better solutions please do let me know.

  • Just moving out gameMapper function to it's own file. I get this error TypeError: _Game.default is not a constructor
  • I then add the init function to the Game class which requires the level argument and initialises the Game class with that level. But I get another error as explained below.

gameMapper.js file

import Game from './Game';

export const gameMapper = [
  { level: 1, nextLevel: Game.init(2) },
  { level: 2, nextLevel: Game.init(3) },
  { level: 3, nextLevel: Game.init(4) },
];

updated Game.js file

import { gameMapper } from './gameMapper';

export default class Game {
  static init = (level) => new Game(level);

  getNextLevel = () => gameMapper.find((g) => g.level === this.level).nextLevel;

}

When I do the above where I move out the gameMapper and import it into the Game.js file I get the following error in gameMapper.js.

TypeError: Cannot read properties of undefined (reading 'init') at { level: 1, nextLevel: Game.init(2) }

I am expecting that the tests should still pass when I move out the mapper.

Thank you all for your precious time :)

about 4 years ago · Juan Pablo Isaza
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