最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Arrow function with Square Brackets? - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 8

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 e.g.

const [someResult, boundStuff] = doSomething()

Or just like any old function e.g.

const something = doSomething()
发布评论

评论列表(0)

  1. 暂无评论