Skip to content

Instantly share code, notes, and snippets.

@tseboho
Created February 8, 2019 06:10
Show Gist options
  • Save tseboho/9242180b1c67a84153535fbe1b0360c7 to your computer and use it in GitHub Desktop.
Save tseboho/9242180b1c67a84153535fbe1b0360c7 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/fakanil
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// MAP, MAP, MAP
/*
* Map was introduced in ES6 as a way of handling key:value pairs.
* A Map is instatiated using the `new` keyword.
* Map exposes Set, Get, Delete & Clear for interacting with the content.
* In the case of iterating we can use Map.keys(), Map.values(), Map.entries()
* Map.entries() is mainly for readability as a Map object can be iterated without entries being called.
*/
'use strict';
var myMap = new Map([
// We can assign pairs at the time of creation.
['a', { a: 'A' }], ['b', { b: 'B' }]]);
// Or use set to assign.
myMap.set('c', { c: 'C' });
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = myMap[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
entry = _step.value;
console.log(entry);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
;
/*
* WeakMap is a Map that allows for garbage collection.
* It does this by keeping a weak reference to entry keys and discudding those that do not get accessed
during the lifecycle of your code.
* WeakMap only allows Objects to be used as keys JS primitives, like strings, do not offer references.
* You cannot iterate or call size() on WeakMap as JS does not know the size of your Map.
*/
var myWeakMap = new WeakMap(); //a
var key1 = { a: 'A' },
key2 = { b: 'B' };
myWeakMap.set(key1, { a: 'A' });
myWeakMap.set(key2, { b: 'B' });
console.log(myWeakMap.get(key1));
// key2 will be garbage collecte / deleted since it is never accessed.
</script>
<script id="jsbin-source-javascript" type="text/javascript">// MAP, MAP, MAP
/*
* Map was introduced in ES6 as a way of handling key:value pairs.
* A Map is instatiated using the `new` keyword.
* Map exposes Set, Get, Delete & Clear for interacting with the content.
* In the case of iterating we can use Map.keys(), Map.values(), Map.entries()
* Map.entries() is mainly for readability as a Map object can be iterated without entries being called.
*/
let myMap = new Map([
// We can assign pairs at the time of creation.
['a', {a: 'A'}],
['b', {b: 'B'}]
]);
// Or use set to assign.
myMap.set('c', {c: 'C'});
for (entry of myMap) {
console.log(entry);
};
/*
* WeakMap is a Map that allows for garbage collection.
* It does this by keeping a weak reference to entry keys and discudding those that do not get accessed
during the lifecycle of your code.
* WeakMap only allows Objects to be used as keys JS primitives, like strings, do not offer references.
* You cannot iterate or call size() on WeakMap as JS does not know the size of your Map.
*/
let myWeakMap = new WeakMap(); //a
const key1 = {a: 'A'}, key2 = {b: 'B'};
myWeakMap.set(key1, {a: 'A'});
myWeakMap.set(key2, {b: 'B'});
console.log(myWeakMap.get(key1))
// key2 will be garbage collecte / deleted since it is never accessed.</script></body>
</html>
// MAP, MAP, MAP
/*
* Map was introduced in ES6 as a way of handling key:value pairs.
* A Map is instatiated using the `new` keyword.
* Map exposes Set, Get, Delete & Clear for interacting with the content.
* In the case of iterating we can use Map.keys(), Map.values(), Map.entries()
* Map.entries() is mainly for readability as a Map object can be iterated without entries being called.
*/
'use strict';
var myMap = new Map([
// We can assign pairs at the time of creation.
['a', { a: 'A' }], ['b', { b: 'B' }]]);
// Or use set to assign.
myMap.set('c', { c: 'C' });
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = myMap[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
entry = _step.value;
console.log(entry);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
;
/*
* WeakMap is a Map that allows for garbage collection.
* It does this by keeping a weak reference to entry keys and discudding those that do not get accessed
during the lifecycle of your code.
* WeakMap only allows Objects to be used as keys JS primitives, like strings, do not offer references.
* You cannot iterate or call size() on WeakMap as JS does not know the size of your Map.
*/
var myWeakMap = new WeakMap(); //a
var key1 = { a: 'A' },
key2 = { b: 'B' };
myWeakMap.set(key1, { a: 'A' });
myWeakMap.set(key2, { b: 'B' });
console.log(myWeakMap.get(key1));
// key2 will be garbage collecte / deleted since it is never accessed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment