0

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

PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
  • 1
    `myObj.request({myParam: null })` returns an array or object. `myObj.request({myParam: null })[something]` returns the element at index `something`. – jabaa Dec 23 '21 at 13:51
  • 1
    as @jabaa I believe the `request` returns an array – frostzt Dec 23 '21 at 13:52
  • 2
    @frostzt and you can still access an array's members with square bracket notation. Arrays are also objects. So, it hardly matters that the result is an array. – VLAZ Dec 23 '21 at 13:57

2 Answers2

1

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

Johannes Merz
  • 3,252
  • 17
  • 33
  • It is the [request package](https://www.npmjs.com/package/request#requestdefaultsoptions). So I guess it's invoking the GET method. and `from` is the first argument. – PlayHardGoPro Dec 23 '21 at 13:57
  • 1
    ah yes, it states in the docs > This method returns a wrapper around the normal request API that defaults to whatever options you pass to it. so you are correct with something = 'get' or any other fn name of the request lib and from are your usual request args followed by the request callback – Johannes Merz Dec 23 '21 at 14:02
1

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.

Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • 2
    It's not a computed property name, it's just plain [bracket notation](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets). More technically called "property accessor". Computed property names come into play when *constructing* an object `obj = {[something]: "hello"}` – VLAZ Dec 23 '21 at 13:59
  • Now i can understand everything. Thank you both! – PlayHardGoPro Dec 23 '21 at 14:04