Does JavaScript have a Map literal notation?

前端 未结 3 1879
时光说笑
时光说笑 2020-12-16 08:56

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

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 09:49

    It's like a HashMap -- there is no literal version. You have to define it like you would a constructor.

    You can read this topic which discusses map literals though, and why they should be added. It's basically others potential proposals on Map literals. I personally can't forsee a literal syntax in ES7, since Maps are very easy to use as is -- but in future proposals there could be syntactic sugar applied.

    An example of potential literal notation was discussed using an octothorp (a hash) to look something like:

    const myMap = Map#{expression("derp"): value("herp")};
    

提交回复
热议问题