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?
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?
Share Improve this question edited Nov 4, 2019 at 21:45 Emile Bergeron 17.4k5 gold badges85 silver badges131 bronze badges asked Nov 4, 2019 at 21:36 chrischris 37k53 gold badges147 silver badges256 bronze badges 1- 1 Doesn't it just return an array? – VLAZ Commented Nov 4, 2019 at 21:37
2 Answers
Reset to default 8This 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 e.g.
const [someResult, boundStuff] = doSomething()
Or just like any old function e.g.
const something = doSomething()