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
CodePudding user response:
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
CodePudding user response:
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.