I am working on some code, and I have stumbled across something I am unfamiliar with.
export const doSomething = () => [ someFunction(), bind(stuff, stuff, stuff) ];
I have never seen an arrow function with a square bracket like that, has anyone else? If so what is the meaning of how it works?
This code means that your function doSomething
returns an array when
[0]
element - the result of execution of function someFunction()
and
[1]
element - the result of execution of function bind(stuff, stuff, stuff)
.
This is a shortcut for:
export const doSomething = () => {
return [ someFunction(), bind(stuff, stuff, stuff) ]
};
But be careful if you want to make a shortcut for returning objects. You have to wrap objects in parentheses ()
, like this:
export const doSomething = () => ({ name: 'John' })
.
It's just returning an array.
You might use it with a destructuring assign eg
const [someResult, boundStuff] = doSomething()
Or just like any old function eg
const something = doSomething()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.