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

145
Visualizações
how to call function with sometimes less argument

I want to create a function such as:

var func = function(arg1, arg2) {
    callAnotherFunc(arg1, arg2);
}

as you can see, when someone needs to call func, it needs to pass 2 args. sometimes, arg2 can be null.

Sometimes, arg2 will be null. Is there some shortcut which allows me to do this ?

var func = function(arg1, arg2) {
    callAnotherFunc(arg1, arg2 || nothing);
}

So if arg2 is null, it shouldn't pass another argument to callAnotherFunc at all. I am looking for some shortcut and not if/else

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

0

I don't understand why you would want to this, maybe your intentions are beyond my understanding. You can just use default parameters

var func = function(arg1, arg2 = null) {
    callAnotherFunc(arg1, arg2);
}

var callAnotherFunc = function(arg1, arg2 = null){
    // console.log(arg1);
    // console.log(arg2);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can try something like this

How To Use ES6 Arguments And Parameters

var func = (...args) => {
    callAnotherFunc(...args);
}

var callAnotherFunc = (...args) =>{
  console.log(...args)
}

func(1);

func(1,2);

func(1,2,3);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could forward all arguments which are not null by using call.

var callAnotherFunc = function(){
  console.log(arguments)
};

var func = function(arg1, arg2){
    //So if arg2 is null, it shouldn't pass another argument to callAnotherFunc at all. 
    callAnotherFunc.call(
      null,
      Array.from(arguments).filter(function(item, index){
        return index == 0 || item !== null
      })
    )
};

func(1, 2);
func(1, null); //REM: Does not pass second argument
func(null, null); //REM: Does not pass second argument

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