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

191
Views
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
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!