admin管理员组文章数量:1356283
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()
本文标签: javascriptArrow function with Square BracketsStack Overflow
版权声明:本文标题:javascript - Arrow function with Square Brackets? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743986931a2571437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论