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

149
Visualizações
Square Brackets after function execution

What is the purpose/ how does it work when declaring a function like:

myObj.request({myParam: null })[something](from, (error, res, body){
   //code
})

What is the behavior of this code? I don't get the meaning of [something] and the anonymous function that follows

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

0

i dont know the implementation details of your posted code so i can just guess:

myObj.request() will return an array/object of functions. With [something] you will select one of those functions and invoke it afterwards. As said, thats only guessing whats going on as you didnt provide any context. For example:

const myObj = {
  request: () => ({ 
    a: () => console.log('i am fn a'), 
    b: () => console.log('i am fn b'), 
  })
}

myObj.request()['a']() // console.logs 'i am fn a'

NOTE: for simplicity reasons i did not pass any arguments/callbacks to the functions. If you still have questions let me know and i'll update the answer

about 4 years ago · Juan Pablo Isaza Relatório

0

Javascript evaluates the expression inside square bracket, in your case the value for something and will executes the function with that name.

For example in your case

myObj.request({myParam: null })[something]

If the value of something is "get" as string. It will simply executes

myObj.request({myParam: null })["get"]

OR

myObj.request({myParam: null }).get

Its just like accessing a prperty inside an object, where myObj.request({myParam: null }) is the object and value for something is the property inside that object.

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