Map.prototype.has()

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 实例的 has() 方法返回一个布尔值,指示此映射中是否存在具有指定键的元素。

¥The has() method of Map instances returns a boolean indicating whether an element with the specified key exists in this map or not.

Try it

语法

¥Syntax

js
has(key)

参数

¥Parameters

key

用于测试 Map 对象中是否存在的元素的键。

返回值

¥Return value

true 如果 Map 对象中存在具有指定键的元素;否则 false

¥true if an element with the specified key exists in the Map object; otherwise false.

示例

¥Examples

使用 has()

¥Using has()

js
const myMap = new Map();
myMap.set("bar", "foo");

console.log(myMap.has("bar")); // true
console.log(myMap.has("baz")); // false

规范

Specification
ECMAScript Language Specification
# sec-map.prototype.has

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看