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

174
Visualizações
ES6 function definition with object destructuring with defaults

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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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});
about 4 years ago · Juan Pablo Isaza Relatório

0

function initNewGame({seed} = {}) {
  seed = seed !== undefined ? seed : randomInt(0, 100000);
  // the rest
}
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