JS WeakMap get()

🌙
手机阅读
本文目录结构

get() 方法返回  WeakMap 指定的元素。

语法

wm.get(key);

参数

key

必须。 想要从 WeakMap 获取的元素的键。

返回值

返回与指定键相关联的值,如果 WeakMap 对象找不到这个键则返回undefined

例子

使用 get 方法

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

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

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)WeakMap.prototype.get Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)WeakMap.prototype.get Draft  

Firefox 中的注意事项

  • Prior to SpiderMonkey 38 (Firefox 38 / Thunderbird 38 / SeaMonkey 2.35), this method threw a TypeError when the key parameter was not an object. However, the latest ES6 standard specifies to return undefined instead. Furthermore, WeakMap.prototype.get accepted an optional second argument as a fallback value, which is not part of the standard. Both non-standard behaviors are removed in version 38 and higher (bug 1127827).

相关链接

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了