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

119
Visualizações
ES6 Partial Object Destructuring Default Parameters In A Separated Object

Okay so there's a lot of similar questions here but none that actually address this. What if I want to save the default parameters of a function on a separated object, in a separated file and then I want the function to use this default parameters only if not specified when calling it?

Example:

export const defaultParameters = { a: "foo", b: "bar"}

And then in another file:

import {defaultParameters} from ./utils.js

myFunction = function(defaultParameters = {}){
   const {a, b} = defaultParameters;
   console.log(a,b);
}

What I want for it to do is that calling

myFunction({a: "hello"})

should print

"hello", "bar"

But that's not how that works and I'm trying to figure this out. Anyone knows how to achieve this?

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

0

You won't be able to do it directly in the parameter, but you can inside the function..

eg..

import {defaultParameters} from ./utils.js

myFunction = function(params = {}){
   const {a, b} = {...defaultParameters, ...params};
   console.log(a,b);
}
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