WeakMap.prototype.get()

WeakMap 实例的 get() 方法返回此 WeakMap 中的指定元素。

¥The get() method of WeakMap instances returns a specified element from this WeakMap.

Try it

语法

¥Syntax

js
get(key)

参数

¥Parameters

key

WeakMap 对象返回的元素的键。

返回值

¥Return value

WeakMap 对象中指定键关联的元素。如果找不到密钥,则返回 undefined。如果 key 不是对象或 未注册符号,则始终返回 undefined

¥The element associated with the specified key in the WeakMap object. If the key can't be found, undefined is returned. Always returns undefined if key is not an object or a non-registered symbol.

示例

¥Examples

使用 get() 方法

¥Using the get() method

js
const wm = new WeakMap();
wm.set(window, "foo");

wm.get(window); // Returns "foo".
wm.get("baz"); // Returns undefined.

规范

Specification
ECMAScript Language Specification
# sec-weakmap.prototype.get

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看