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

111
Visualizações
How to check the number of arguments provided to a callback?

I am trying to implement my version of forEach to make sure I understand it correctly, however, since the callback provided to forEach can accept 1, 2 or 3 arguments I need to figure out a way to check the number of arguments.

For example, here's what I currently have:

Array.prototype.myForEach = (callback) => {
  //console.log(callback.arguments.length, "arguments supplied to callback") This doesn't work.

  //Check the number of arguments provided to the callback

  if(typeof callback === 'function') {
    for(var i = 0; i < this.length; i++) {
      //If single argument then callback(this[i]) should be invoked.
      callback(this[i], i, this)
    }
  }
  else throw new Error("Callback for myForEach is not a function")
}

What I aim to achieve is:

arr.myForEach((a, b) => {} //a should refer to element, b to the index, c to the array

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

0

While you can check the number of arguments provided by checking the .length property of the function:

Array.prototype.myForEach = (callback) => {
  console.log(callback.length);
};

[].myForEach((arg1, arg2) => {});
[].myForEach((arg1) => {});

It's not needed here - there's no problem with passing all arguments regardless of the callback uses them or not (and if the callback accepts more than 3 arguments, for some odd reason, they'll be undefined inside the callback, just like with forEach).

Array.prototype.myForEach = function(callback) {
  if (typeof callback !== 'function') {
    throw new Error("Callback for myForEach is not a function")
  }
  for(var i = 0; i < this.length; i++) {
    callback(this[i], i, this);
  }
};

['a', 'b'].myForEach((item, i, arr) => {
  console.log(item);
  console.log(i);
  console.log(arr);
});

about 4 years ago · Juan Pablo Isaza Relatório

0

The number of arguments passed will be reflected by the callback.length

Array.prototype.myForEach = (callback) => {
    console.log(callback.length);
}

const arr = [1,1];
arr.myForEach((arg1, arg2, arg3) => {}); //3
arr.myForEach((arg1, arg2, arg3, arg4) => {}); //4
arr.myForEach((arg1, arg2, arg3, arg4, arg5) => {}); //5

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