Map() 构造函数

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Map() 构造函数创建 Map 对象。

¥The Map() constructor creates Map objects.

语法

¥Syntax

js
new Map()
new Map(iterable)

注意:Map() 只能与 new 一起构建。尝试在没有 new 的情况下调用它会抛出 TypeError

¥Note: Map() can only be constructed with new. Attempting to call it without new throws a TypeError.

参数

¥Parameters

iterable Optional

一个 Array 或其他 iterable 对象,其元素是键值对。(例如,具有两个元素的数组,例如 [[ 1, 'one' ],[ 2, 'two' ]]。)每个键值对都会添加到新的 Map 中。

示例

¥Examples

创建新映射

¥Creating a new Map

js
const myMap = new Map([
  [1, "one"],
  [2, "two"],
  [3, "three"],
]);

规范

Specification
ECMAScript Language Specification
# sec-map-constructor

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看