JavaScript 中是否有 Set 文字?
Is there a Set literal in JavaScript?
我可以 make a Set with new Set()
,我可以使用 Array 或 Object 或 Boolean 或 Number 构造函数。
但是是否有一套文字语法,就像数组、对象、布尔值、数字等一样?
不,没有用于声明 Set
的单一语法。如有疑问,consult the spec.
嗯,Set()
没有文字语法,但您可以改用数组。它们都非常相似,可以通过使用以下功能轻松切换:
Array.from(mySet) // Converts mySet into an array
new Set(myArray) // Creates a set from myArray
正如其他人所指出的,还没有 Set(或 Map)文字语法。在这个 ES Discuss Thread and in the following twitter discussion.
中有一些想法浮出水面
一些建议的语法示例:
const set = {<1, "two", false>}; // by Brendan Eich
const set = {. 1, "two", false .}; // by Axel Rauschmayer
不过,据我所知,目前还没有实施其中任何一个的提案。
我可以 make a Set with new Set()
,我可以使用 Array 或 Object 或 Boolean 或 Number 构造函数。
但是是否有一套文字语法,就像数组、对象、布尔值、数字等一样?
不,没有用于声明 Set
的单一语法。如有疑问,consult the spec.
嗯,Set()
没有文字语法,但您可以改用数组。它们都非常相似,可以通过使用以下功能轻松切换:
Array.from(mySet) // Converts mySet into an array
new Set(myArray) // Creates a set from myArray
正如其他人所指出的,还没有 Set(或 Map)文字语法。在这个 ES Discuss Thread and in the following twitter discussion.
中有一些想法浮出水面一些建议的语法示例:
const set = {<1, "two", false>}; // by Brendan Eich
const set = {. 1, "two", false .}; // by Axel Rauschmayer
不过,据我所知,目前还没有实施其中任何一个的提案。