-1

So I have this function that has the following JSDoc

/**
 * Initializes a new game, this means creating a new map and centering on the
 * playerbase
 *
 * @param {object} param1
 * @param {number} param1.seed
 * */

Currently I have this as the function definition

initNewGame({ seed }) {}

But I would like to have a default value for seed, which is randomInt(0, 100000). How can I achieve this.

Daan Breur
  • 351
  • 3
  • 12

2 Answers2

2

You could do the following:

function initNewGame(params, seed = randomInt(0, 100000)) {
   //body of the function
}

this can then be called like this:

let params = {}
let seed = 4124;
initNewGame(params);
//or 
initNewGame(params, seed);

Unfortunately this means that the JSDoc should change accordingly, since seed is not a property of props anymore.

EDIT: another thing you can do is:

function initNewGame(params = {anyvalue: "hello", seed: getRandomInt(1000)}) {
  console.log(params.seed)
}

initNewGame();
//or
initNewGame({anyvalue: "hello in call", seed: 1000});
Rensykes
  • 64
  • 8
0
function initNewGame({seed} = {}) {
  seed = seed !== undefined ? seed : randomInt(0, 100000);
  // the rest
}
bryan60
  • 28,215
  • 4
  • 48
  • 65