WeakMap 对象是一组键/值对的集合,其中的键是弱引用的。其键必须是对象,而值可以是任意的。

 

你可以在这里了解更多关于WeakMap的内容:WeakMap object in 带键的集合.

 

语法

new WeakMap([iterable])

参数

iterable
Iterable 是一个数组(2元数组)或者其他可迭代的且其元素是键值对的对象。每个键值对会被加到新的 WeakMap 里。null 会被当做 undefined。

描述

WeakMap 的 key 只能是 Object 类型。 原始数据类型 是不能作为 key 的(比如 Symbol)。

Why WeakMap?

在 JavaScript 里,map API 可以通过在其四个 API 方法中共用两个数组(一个存放键,一个存放值)来实现。给该 map 设置值时会同时将键和值推到这两个数组的末尾。从而使得键和值的索引在两个数组中相对应。当从该 map 取值的时候,需要遍历所有的键,然后使用此索引从存储值的数组中检索出相应的值。

但这样的实现会有两个很大的缺点,首先是O(n)的时间复杂度(n是键值对的个数)。另外一个则可能会导致内存泄漏:在这种自己实现的 WeakMap 中,存放键的数组中的每个索引将会保持对所引用对象的引用,阻止他们被当作垃圾回收.

而在原生的WeakMap中,每个键对自己所引用对象的引用是 "弱引用", 这意味着,如果没有其他引用和该键引用同一个对象,这个对象将会被当作垃圾回收。原生 WeakMap 的结构是特殊且有效的,其用于映射的 key 只有在其没有被回收时才是有效的。

正由于这样的弱引用,WeakMap 的 key 是不可枚举的 (没有方法能给出所有的 key)。如果key 是可枚举的话,其列表将会受垃圾回收机制的影响,从而得到不确定的结果. 因此,如果你想要这种类型对象的 key 值的列表,你应该使用 Map

属性

WeakMap.length
length  属性的值为 0。
WeakMap.prototype
WeakMap 构造器的原型。 允许添加属性到所有的 WeakMap 对象。

WeakMap 实例

所有 WeakMap 实例继承自 WeakMap.prototype.

属性

WeakMap.prototype.constructor
返回创建WeakMap实例的原型函数。 WeakMap函数是默认的。

方法

WeakMap.prototype.delete(key)
移除key的关联对象。执行后 WeakMap.prototype.has(key)返回false。
WeakMap.prototype.get(key)
返回key关联对象, 或者 undefined(没有key关联对象时)。
WeakMap.prototype.has(key)
根据是否有key关联对象返回一个Boolean值。
WeakMap.prototype.set(key, value)
在WeakMap中设置一组key关联对象,返回这个 WeakMap对象。
WeakMap.prototype.clear()
WeakMap中移除所有的 key/value 。 注意,该方法已弃用,但可以通过创建一个空的WeakMap并替换原对象来实现 (参看 WeakMap的后半部分)

示例

使用 WeakMap

var wm1 = new WeakMap(),
    wm2 = new WeakMap(),
    wm3 = new WeakMap();
var o1 = {},
    o2 = function(){},
    o3 = window;

wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // value可以是任意值,包括一个对象
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // 键和值可以是任意对象,甚至另外一个WeakMap对象
wm1.get(o2); // "azerty"
wm2.get(o2); // undefined,wm2中没有o2这个键
wm2.get(o3); // undefined,值就是undefined

wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true (即使值是undefined)

wm3.set(o1, 37);
wm3.get(o1); // 37
wm3.clear();
wm3.get(o1); // undefined,wm3已被清空
wm1.has(o1);   // true
wm1.delete(o1);
wm1.has(o1);   // false

用 .clear() 方法实现伪 WeakMap

为了更好的说明,下面使用了 ECMAScript6 新增的 class 构造函数,其目前没有广泛实现。

class ClearableWeakMap {
  constructor(init) {
    this._wm = new WeakMap(init)
  }
  clear() {
    this._wm = new WeakMap()
  }
  delete(k) {
    return this._wm.delete(k)
  }
  get(k) {
    return this._wm.get(k)
  }
  has(k) {
    return this._wm.has(k)
  }
  set(k, v) {
    this._wm.set(k, v)
    return this
  }
}

规范

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

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 36Edge Full support 12Firefox Full support 6IE Full support 11Opera Full support 23Safari Full support 8WebView Android Full support 36Chrome Android Full support 36Edge Mobile Full support 12Firefox Android Full support 6Opera Android Full support 23Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
new WeakMap(iterable)Chrome Full support 38Edge Full support 12Firefox Full support 36IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 36Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
new WeakMap(null)Chrome Full support YesEdge Full support 12Firefox Full support 37IE Full support 11Opera ? Safari Full support 8WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 37Opera Android ? Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
WeakMap() without new throwsChrome Full support YesEdge Full support 12Firefox Full support 42IE Full support 11Opera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 42Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
clear
DeprecatedNon-standard
Chrome No support 36 — 43Edge No support NoFirefox No support 20 — 46IE Full support 11Opera No support 25 — 30Safari No support 8 — 9WebView Android No support 36 — 43Chrome Android No support 36 — 43Edge Mobile No support NoFirefox Android No support 20 — 46Opera Android No support 25 — 30Safari iOS No support 8 — 9Samsung Internet Android Full support Yesnodejs Full support Yes
deleteChrome Full support 36Edge Full support YesFirefox Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
IE Full support 11Opera Full support 23Safari Full support 8WebView Android Full support 36Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
Opera Android Full support 23Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
getChrome Full support 36Edge Full support YesFirefox Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. However, the ES2015 specification 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.
IE Full support 11Opera Full support 23Safari Full support 8WebView Android Full support 36Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. However, the ES2015 specification 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.
Opera Android Full support 23Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
hasChrome Full support 36Edge Full support YesFirefox Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
IE Full support 11Opera Full support 23Safari Full support 8WebView Android Full support 36Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
Opera Android Full support 23Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
prototypeChrome Full support 36Edge Full support YesFirefox Full support 6IE Full support 11Opera Full support 23Safari Full support 8WebView Android Full support 36Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 6Opera Android Full support 23Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
setChrome Full support 36Edge Full support YesFirefox Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
IE Partial support 11
Notes
Partial support 11
Notes
Notes Returns 'undefined' instead of the 'Map' object.
Opera Full support 23Safari Full support 8WebView Android Full support 36Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
Opera Android Full support 23Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

相关链接

文档标签和贡献者

最后编辑者: BingerWeb,