Is there a Set literal in JavaScript?

2019-06-15 02:15发布

I can make a Set with new Set(), same way I can use the Array or Object or Boolean or Number constructors.

But is there a set literal syntax, like there is for arrays, objects, booleans, numbers etc?

3条回答
等我变得足够好
2楼-- · 2019-06-15 02:30

Nope, there's no single syntax for declaring a Set. When in doubt, consult the spec.

查看更多
【Aperson】
3楼-- · 2019-06-15 02:32

As others have pointed out, there is no Set (or Map) literal syntax yet. There have been a few ideas floating around, in this ES Discuss Thread and in the following twitter discussion.

A few proposed syntax examples:

const set = {<1, "two", false>}; // by Brendan Eich
const set = {. 1, "two", false .}; // by Axel Rauschmayer

There are, as far as I can see, no proposals to implement any of them yet, though.

查看更多
Animai°情兽
4楼-- · 2019-06-15 02:35

Well, there's no literal syntax for Set() but you can use an array instead. They are both very similar and can easily be switched between from using the functions:

Array.from(mySet) // Converts mySet into an array
new Set(myArray) // Creates a set from myArray, yes I know
查看更多
登录 后发表回答