Skip to content

Instantly share code, notes, and snippets.

@tseboho
Created February 8, 2019 06:42
Show Gist options
  • Save tseboho/311032934c830c51cdbf5f4c601d7fd9 to your computer and use it in GitHub Desktop.
Save tseboho/311032934c830c51cdbf5f4c601d7fd9 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zemeyif
<!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">
// SET, SET, SET!
/*
* Set was introduced in ES6 and the allow us to store a list of values
* Unlike Arrays, Sets only hold unique values
*/
let myArray = [1, 1, 1, 2];
console.log('myArray');
for (entry of myArray) {
console.log(entry);
}
let mySet = new Set(myArray);
mySet.add(3);
console.log('mySet');
for (entry of mySet) {
console.log(entry);
}
/*
* We can use Set.delete(value)
- Set also supports clear(), similar to Map
*/
mySet.delete(3)
console.log('mySet');
for (entry of mySet) {
console.log(entry);
}
// Set do no allow `get` as this doesn't make much sense, we instead have Set.has(value)
console.log('has 2', mySet.has(2));
console.log('has 3', mySet.has(3));
// We can iterate our Set using Set.entries(), Set.keys(), Set.values()
console.log('mySet.entries', mySet.entries());
console.log('mySet.keys()', mySet.keys());
console.log('mySet.values()', mySet.values());
// WeakSet are similar to Set sans being iterable and only storing values as Objects.
let obj1 = {a: 1};
let obj2 = {b: 2};
let myWeakSet = new WeakSet([obj1, obj2, {b: 2}]);
// Gotcha: The third object would be added to the set as it would be refering to a diffent object in memory.
console.log('myWeakSet has {b: 2}', myWeakSet.has({b: 2}));
// Gotcha: In this case too, {b: 2} would reference a different object in memory.
console.log('myWeakSet has obj2', myWeakSet.has(obj2));
</script>
<script id="jsbin-source-javascript" type="text/javascript">// SET, SET, SET!
/*
* Set was introduced in ES6 and the allow us to store a list of values
* Unlike Arrays, Sets only hold unique values
*/
let myArray = [1, 1, 1, 2];
console.log('myArray');
for (entry of myArray) {
console.log(entry);
}
let mySet = new Set(myArray);
mySet.add(3);
console.log('mySet');
for (entry of mySet) {
console.log(entry);
}
/*
* We can use Set.delete(value)
- Set also supports clear(), similar to Map
*/
mySet.delete(3)
console.log('mySet');
for (entry of mySet) {
console.log(entry);
}
// Set do no allow `get` as this doesn't make much sense, we instead have Set.has(value)
console.log('has 2', mySet.has(2));
console.log('has 3', mySet.has(3));
// We can iterate our Set using Set.entries(), Set.keys(), Set.values()
console.log('mySet.entries', mySet.entries());
console.log('mySet.keys()', mySet.keys());
console.log('mySet.values()', mySet.values());
// WeakSet are similar to Set sans being iterable and only storing values as Objects.
let obj1 = {a: 1};
let obj2 = {b: 2};
let myWeakSet = new WeakSet([obj1, obj2, {b: 2}]);
// Gotcha: The third object would be added to the set as it would be refering to a diffent object in memory.
console.log('myWeakSet has {b: 2}', myWeakSet.has({b: 2}));
// Gotcha: In this case too, {b: 2} would reference a different object in memory.
console.log('myWeakSet has obj2', myWeakSet.has(obj2));
</script></body>
</html>
// SET, SET, SET!
/*
* Set was introduced in ES6 and the allow us to store a list of values
* Unlike Arrays, Sets only hold unique values
*/
let myArray = [1, 1, 1, 2];
console.log('myArray');
for (entry of myArray) {
console.log(entry);
}
let mySet = new Set(myArray);
mySet.add(3);
console.log('mySet');
for (entry of mySet) {
console.log(entry);
}
/*
* We can use Set.delete(value)
- Set also supports clear(), similar to Map
*/
mySet.delete(3)
console.log('mySet');
for (entry of mySet) {
console.log(entry);
}
// Set do no allow `get` as this doesn't make much sense, we instead have Set.has(value)
console.log('has 2', mySet.has(2));
console.log('has 3', mySet.has(3));
// We can iterate our Set using Set.entries(), Set.keys(), Set.values()
console.log('mySet.entries', mySet.entries());
console.log('mySet.keys()', mySet.keys());
console.log('mySet.values()', mySet.values());
// WeakSet are similar to Set sans being iterable and only storing values as Objects.
let obj1 = {a: 1};
let obj2 = {b: 2};
let myWeakSet = new WeakSet([obj1, obj2, {b: 2}]);
// Gotcha: The third object would be added to the set as it would be refering to a diffent object in memory.
console.log('myWeakSet has {b: 2}', myWeakSet.has({b: 2}));
// Gotcha: In this case too, {b: 2} would reference a different object in memory.
console.log('myWeakSet has obj2', myWeakSet.has(obj2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment