WeakMap
WeakMap 对象是一组键/值对的集合,其中的键是弱引用的。其键必须是对象,而值可以是任意的。
语法
new WeakMap([iterable])参数
iterableIterable 是一个数组(2元数组)或者可遍历的且其元素是键值对的对象。每个键值对会被加到新的 WeakMap 里。null 会被当做 undefined。
描述
Why _Weak_Map?
在 JavaScript 里,可以通过四个共用两个数组(一个存放键,一个存放值)的 API 方法来实现 map API。给该 map 设置值时会同时将键和值推到这两个数组的末尾。从而使得键和值的索引在两个数组中相对应。当从该 map 取值的时候,需要遍历所有的键,然后使用此索引从存储值的数组中检索出相应的值。
正由于这样的弱引用,WeakMap的 key 是非枚举的 (没有方法能给出所有的 key)。如果key 是可枚举的话,其列表将会受垃圾回收机制的影响,从而得到不确定的结果. 因此,如果你想要这种类型对象的 key 值的列表,你应该使用Map。
属性
WeakMap.lengthlength 属性的值为 0。
WeakMap.prototypeWeakMap构造器的原型。 允许添加属性到所有的 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); // a value can be anything, including an object or a function
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // keys and values can be any objects. Even WeakMaps!
wm1.get(o2); // "azerty"
wm2.get(o2); // undefined, because there is no key for o2 on wm2
wm2.get(o3); // undefined, because that is the set value
wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true (even if the value itself is 'undefined')
wm3.set(o1, 37);
wm3.get(o1); // 37
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)The definition of 'WeakMap' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'WeakMap' in that specification. | Living Standard | |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
Basic support | 36 | 12 | 6.0 (6.0) | 11 | 23 | 7.1 |
new WeakMap(iterable) | 38 | 12 | 36 (36) | No support | 25 | 9 |
Obsolete clear() method removed | 43 | 12 | 46 (46) | No support | 30 | 9 |
Constructor argument: new WeakMap(null) | (Yes) | 12 | 37 (37) | 11 | ? | 8 (7.1?) |
WeakMap() without new throws | (Yes) | 12 | 42 (42) | 11 | ? | ? |
Feature | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
Basic support | 35 | (Yes) | 6.0 (6.0) | No support | No support | 8 |
new WeakMap(iterable) | 38 | (Yes) | 36.0 (36) | No support | No support | 9 |
Obsolete clear() method removed | 43 | (Yes) | ? | No support | 30 | 9 |
Constructor argument: new WeakMap(null) | ? | (Yes) | 37.0 (37) | No support | ? | 8 |
WeakMap() without new throws | ? | (Yes) | 42.0 (42) | No support | ? | ? |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

