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

230
Visualizações
Need Explanation about Array.prototype.map and map.call( ), how this is working?
let Map = Array.prototype.map
let strResult = Map.call("Muhammad asif", (x) => {
    return x.charCodeAt(0);
})
console.log(strResult)

What is the relation between call and map and how is its process? why charCodeAt(0) used here?

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

0

  1. What is the relation between call and map?
  • As @jabaa said in the comments section, map is a function. call also a function.
  • map is a part of Array object, and call is a part of Function object.
  • It means,
    • you should use map on an array. For example: [1, 2, 3].map(),
    • and use call on a function. For example: myFunction.call()

  1. How is its process?

Array.prototype.map is a function that you put on a variable called Map. You can't just use it like a normal function, because it is designer to be used with an array. You can use it like this:

const myArray = [1, 2, 3]
myArray.Map = Map // put your `Map` variable into a real array
myArray.Map(number => console.log(number))

OR

Map.call("your array or iterable things goes here", (itemInArray) => {console.log(itemInArray)});
/* first parameter of `call` will be used as `this`. `this` in `Array.prototype.map` means an array. CMIIW */

Which is the same as:

const myString = "your array or iterable things goes here"
for(let index = 0; index < myString.length; index++){
  const itemInArray = myString[index]
  console.log(itemInArray)
}

  1. Why charCodeAt(0) used here?

I don't know. That is your code, and the one who should answer this question is yourself. charCodeAt is not a required function for map or call to works. FYI, charCodeAt is also a function that returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index. So,

"abc".charCodeAt(0) // will show UTF-16 representation of the 0th character of "abc"
"abc".charCodeAt(1) // will show UTF-16 representation of the 1st character of "abc"
"abc".charCodeAt(2) // will show UTF-16 representation of the 2nd character of "abc"
about 4 years ago · Juan Pablo Isaza Relatório

0

Here you can find some explanation https://forum.freecodecamp.org/t/explain-array-prototype-map-call/165936/7

With this approach, you can use map function on string as it was an array.

charCodeAt is the function called on each element of the given string. It returns the Unicode representation (a number) of passed character. You can pass any other string-accepting function istead of it.

For example:

let Map = Array.prototype.map
let strResult = Map.call("Muhammad asif", (x) => {
    return x + x
});
console.log(strResult)

will result with ["MM", "uu", "hh", "aa", "mm", "mm", "aa", "dd", " ", "aa", "ss", "ii", "ff"]

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