WeakMap
object is a collection of key/value pairs in which the keys are weakly referenced. The keys must be objects and the values can be arbitrary values.
You can learn more about
WeakMap
s in the
WeakMap object
guide (under
键控集合
).
Keys of WeakMaps are of the type
Object
only.
Primitive data types
as keys are not allowed (e.g. a
Symbol
can't be a
WeakMap
key).
A map API could be implemented in JavaScript with two arrays (one for keys, one for values) shared by the four API methods. Setting elements on this map would involve pushing a key and value onto the end of each of those arrays simultaneously. As a result, the indices of the key and value would correspond to both arrays. Getting values from the map would involve iterating through all keys to find a match, then using the index of this match to retrieve the corresponding value from the array of values.
Such an implementation would have two main inconveniences:
By contrast, native
WeakMap
s hold "weak" references to key objects, which means that they do not prevent garbage collection in case there would be no other reference to the key object. This also avoids preventing garbage collection of values in the map. Native WeakMaps can be particularly useful constructs when mapping keys to information about the key that is valuable only if the key has not been garbage collected.
Because the references are weak,
WeakMap
keys are not enumerable.
There is no method to obtain a list of the keys. If they were, the list would depend on the state of garbage collection, introducing non-determinism. If you want to have a list of keys, you should use a
Map
.
WeakMap()
WeakMap
对象。
WeakMap.prototype.delete(
key
)
key
.
WeakMap.prototype.has(
key
)
will return
false
afterwards.
WeakMap.prototype.get(
key
)
key
,或
undefined
若没有。
WeakMap.prototype.has(
key
)
key
在
WeakMap
object or not.
WeakMap.prototype.set(
key
,
value
)
value
为
key
在
WeakMap
object. Returns the
WeakMap
对象。
const wm1 = new WeakMap(),
wm2 = new WeakMap(),
wm3 = new WeakMap();
const 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
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;
}
}
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'WeakMap' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
WeakMap
|
Chrome 36 | Edge 12 | Firefox 6 | IE 11 | Opera 23 | Safari 8 | WebView Android 37 | Chrome Android 36 | Firefox Android 6 | Opera Android 24 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
WeakMap()
构造函数
|
Chrome 36 | Edge 12 | Firefox 6 | IE 11 | Opera 23 | Safari 8 | WebView Android 37 | Chrome Android 36 | Firefox Android 6 | Opera Android 24 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
clear
弃用
非标
|
Chrome 36 — 43 | Edge No | Firefox 20 — 46 | IE 11 | Opera 25 — 30 | Safari 8 — 9 | WebView Android 37 — 43 | Chrome Android 36 — 43 | Firefox Android 20 — 46 | Opera Android 25 — 30 | Safari iOS 8 — 9 | Samsung Internet Android 3.0 — 4.0 | nodejs 0.12 — 4.0.0 |
delete
|
Chrome 36 | Edge 12 |
Firefox
6
|
IE 11 | Opera 23 | Safari 8 | WebView Android 37 | Chrome Android 36 |
Firefox Android
6
|
Opera Android 24 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
get
|
Chrome 36 | Edge 12 |
Firefox
6
|
IE 11 | Opera 23 | Safari 8 | WebView Android 37 | Chrome Android 36 |
Firefox Android
6
|
Opera Android 24 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
has
|
Chrome 36 | Edge 12 |
Firefox
6
|
IE 11 | Opera 23 | Safari 8 | WebView Android 37 | Chrome Android 36 |
Firefox Android
6
|
Opera Android 24 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
set
|
Chrome 36 | Edge 12 |
Firefox
6
|
IE
部分支持
11
|
Opera 23 | Safari 8 | WebView Android 37 | Chrome Android 36 |
Firefox Android
6
|
Opera Android 24 | Safari iOS 8 | Samsung Internet Android 3.0 |
nodejs
0.12
|
完整支持
部分支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。
WeakMap
in the JavaScript guide
Map
Set
WeakSet
WeakMap
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()