JavaScript是否具有地图文字符号? [英] Does JavaScript have a Map literal notation?
问题描述
自ES6起,JavaScript具有适当的Map对象。我没有看到使用字面符号的方法,尽管如此,您可以使用数组或对象。我错过了,还是不存在?
As of ES6, JavaScript has a proper Map object. I don't see a way to use a literal notation though, as you could with an Array or an Object. Am I missing it, or does it not exist?
数组: var arr = [Foo,Bar];
Object: var obj = {foo:Foo,bar:Bar};
地图:
推荐答案
不,ES6没有地图
s或设置
s的文字符号。
No, ES6 does not have a literal notation for Map
s or Set
s.
您将不得不使用它们的构造函数,传递一个可迭代的(通常为数组文字):
You will have to use their constructors, passing an iterable (typically an array literal):
var map = new Map([["foo", "Foo"], ["bar", "Bar"], …]);
var set = new Set(["Foo", "Bar", …]);
有一些建议,为语言添加新的文字语法,但没有一个提出到ES6(和我个人并不自信,他们会将其纳入任何未来的版本)。
There are some proposals to add new literal syntax to the language, but none made it into ES6 (and I'm personally not confident they will make it into any future version).
这篇关于JavaScript是否具有地图文字符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!