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

304
Visualizações
Explain me callback and how its refer to array in code

I dont understand what is callback in this example, espicially line newArray.push(callback(this[i])); as i got it (this[i]) is item from Array, but how does CALLBACK refer to code;

const s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
  const newArray = [];
  
  for(let i=0;i<this.length;i++){
      newArray.push(callback(this[i]));
  }
  return newArray;
};



const new_s = s.myMap(function(item) {
  return item * 2;
});

console.log(new_s);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

1. Background concepts

Firstly, this top part is just a function definition where 'callback' is just a parameter that myMap is able to accept. The word 'callback' isn't special and you can use in fact any name, but 'callback' does signal that in your function definition you are asking the caller to supply a function instead of an integer or a string. That is, you could also write something like this:

Array.prototype.myMap = function(param) {
   // you can console.log(param) and you would see the parameter in log.
}

And in theory you could then call this function by doing:

s.myMap(1) // the log will show 1
s.myMap("hello") // the log will show "hello"
s.myMap(function() {}) // the log will show [Parameter is a Function]

Secondly, if you name your parameter 'callback', it signals to the caller that they could in fact pass a function into this myMap not just an integer or a string -- so when you write something like this:

Array.prototype.myMap = function(callback_f) {
   callback_f(); // <----- call the incoming function passed as a param
}

Then the caller has an idea that they have to supply a function into myMap, either in this way:

s.MyMap(function() {
   // do some stuff
})

Or in this way:

function doStuff() {}
s.MyMap(doStuff)

Either way, the parameter callback_f is expected to be a function in this case, and myMap will call and execute this function, regardless of what you pass into it.

2. Answering your question

As you may already know, this is a special function definition because, by doing Array.prototype.myMap you're modifying how all arrays work and all arrays will now gain this function definition myMap.

Secondly, you can call this function by doing s.myMap() if s is any array.

So in your case, the line:

newArray.push(callback(this[i]))

could also be written as:

let result_of_executing_the_callback = callback(this[i])
newArray.push(result_of_executing_the_callback)

which means: first, execute the incoming callback function on this (= current array) at the index i. And what is the incoming callback function? It is the function f that you are passing in when you do s.MyMap(f):

In your case f is this:

function(item) {
  return item * 2;
}

picture of f being passed into your function as the parameter 'callback'

(If this is helpful please mark as accepted!)

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