What is weakmap-polyfill?
The weakmap-polyfill npm package provides a polyfill for the WeakMap object, which is a collection of key/value pairs where the keys are objects and the values can be arbitrary values. This polyfill is useful for environments that do not natively support WeakMap.
What are weakmap-polyfill's main functionalities?
Creating a WeakMap
This feature allows you to create a new instance of a WeakMap using the polyfill.
const WeakMapPolyfill = require('weakmap-polyfill');
const wm = new WeakMapPolyfill();
Setting a value
This feature allows you to set a value in the WeakMap for a specific object key.
const obj = {};
wm.set(obj, 'value');
Getting a value
This feature allows you to retrieve a value from the WeakMap using an object key.
const value = wm.get(obj);
Checking for a key
This feature allows you to check if a specific object key exists in the WeakMap.
const hasKey = wm.has(obj);
Deleting a key
This feature allows you to remove a key/value pair from the WeakMap.
wm.delete(obj);
Other packages similar to weakmap-polyfill
core-js
core-js is a modular standard library for JavaScript that includes polyfills for various ECMAScript features, including WeakMap. It provides a comprehensive set of polyfills for modern JavaScript features, making it a more extensive solution compared to weakmap-polyfill.
es6-weak-map
es6-weak-map is another polyfill for the WeakMap object. It provides similar functionality to weakmap-polyfill but is specifically focused on WeakMap, making it a more lightweight alternative to core-js.
weakmap-polyfill
ECMAScript6 WeakMap polyfill.
Installation
npm
$ npm install --save weakmap-polyfill
Usage
Import or require weakmap-polyfill
, then WeakMap will be defined in the global scope if native WeakMap is not supported in running environment.
using import
import 'weakmap-polyfill';
const weakMap = new WeakMap();
using require
require('weakmap-polyfill');
var weakMap = new WeakMap();
browser (standalone)
<script src="weakmap-polyfill.min.js"></script>
<script>
var weakMap = new WeakMap();
</script>
Compatibility
- Chrome 15
- Firefox 3
- IE 7
- Safari 4
- Opera 11.5
- Edge
Browser Tests
Limitations
This polyfill has following few limitations.
- WeakMap
iterable
argument is not supported. (23.3.1.1 WeakMap ( [ iterable ] )) - Frozen and sealed objects are not supported.
- The values held by a WeakMap can't be collected once the map itself is GCed, since the values here are tied to the keys. #4
License
MIT