WeakMap()
构造函数
creates
WeakMap
objects which are a collections 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 section
WeakMap object
in
键控集合
.
new WeakMap([iterable])
iterable
Iterable is an Array or other iterable object whose elements are key-value pairs (2-element Arrays). Each key-value pair will be added to the new WeakMap. null is treated as undefined.
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
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'WeakMap constructor' 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
|
new WeakMap(iterable)
|
Chrome 38 | Edge 12 | Firefox 36 | IE No | Opera 25 | Safari 9 | WebView Android 38 | Chrome Android 38 | Firefox Android 36 | Opera Android 25 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
WeakMap()
without
new
throws
|
Chrome 36 | Edge 12 | Firefox 42 | IE 11 | Opera 23 | Safari 9 | WebView Android 37 | Chrome Android 36 | Firefox Android 42 | Opera Android 24 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
new WeakMap(null)
|
Chrome 36 | Edge 12 | Firefox 37 | IE 11 | Opera 23 | Safari 8 | WebView Android 37 | Chrome Android 36 | Firefox Android 37 | 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()