WeakMap.prototype.get()
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.
WeakMap
实例的 get()
方法返回该 WeakMap
中的指定元素。
尝试一下
const weakmap1 = new WeakMap();
const object1 = {};
const object2 = {};
weakmap1.set(object1, 42);
console.log(weakmap1.get(object1));
// Expected output: 42
console.log(weakmap1.get(object2));
// Expected output: undefined
语法
js
get(key)
参数
key
-
要从
WeakMap
对象返回的元素的键。
返回值
示例
使用 get() 方法
js
const wm = new WeakMap();
wm.set(window, "foo");
wm.get(window); // 返回 "foo"。
wm.get("baz"); // 返回 undefined。
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-weakmap.prototype.get |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
get |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.