ES6 Data Type
Disclaimer
This article is originally based on my understanding of this concept. No guarantee on the accuracy.😅
A summary of data type that introduced in ES6.
Type | Symbol | Map | Set |
---|---|---|---|
Summary | Unique ‘string’ | Ordered ‘object’ whose key can be primitive & reference | ‘Array’ with all unique value |
Construct | let sb = Symbol() | let m = new Map([iteratble]) | let s = new Set([iteratble]) |
Key-value | Description as ‘key’ Symbol.keyFor(sb) |
key=>any type ordered value=>any type |
key<=>value unique key |
Loop | N\A | For…of forEach() | for…of forEach() |
Transfer | sb.toString() | […m] = Array.from(m) <=> new Map(arr). | […s] = Array.from(s) <=> new Set(arr). |
Symbol
An unique ‘string’ that would never be equvlient to others
primitive data type
Symbol(< description >)
function returns an anonymous, unique value1
Symbol('foo') === Symbol('foo') // false
This functon has its static propertise and methods
1
2
3
4let sb = Symbol.for('foo')
let sb2= Symbol.for('foo') //saved the value in global env in the memory
sb ===sb2//true
Symbol.keyFor(sb) //'foo'Not
new Symbol()
❌1
let sym = new Symbol() // TypeError
1
2
3
4let sym = Symbol('foo')
typeof sym // "symbol"
let symObj = Object(sym)
typeof symObj // "object"But it has instance/prototype properties and methods
1
2
3Symbol.prototype.toString()
let sb = Symbol('context');
console.log(sb.description); //contextReturns a string containing the description of the Symbol. Overrides the Object.prototype.toString() method.
Use cases
To dealing with the same string name, use symbol to create unique value;
1 | let user1= { |
Using symbol:
1 | let user1= { |
for...in
can only get the propertise of an object that its key is not a symbol.
The Object.getOwnPropertySymbols()
method can only get the symbol peroproty
1 | let sb = Symbol('b') |
The symbol can protect the propertise to be looped from outside.
Map
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
An order object whose key can be any data type
- iterable, can be looped through
for...of
- Its size can be easily obtained by the
size
property - use
new Map([iterable])
to create a Map object where the key-value of each iterable were pushed into the Map - Use
Map.prototype.set([key])
to push a new value, use.get([key])
to get the value Map.prototype.has()
return boolean- The key has to be unique. No repeating keys. The later insertion would overwrite the earlier one.
1 | let obj={a:'a'} |
- These methods return a new iterator object contains each key/value/entry in the map
1 | [...m] // [[{},'user'],[f,'function'],[{...},'b']] |
Map.prototype.forEach(callbackFn[, thisArg])
can loop the map
1 | m.forEach((value)=>{ |
Compared with object
- An
Object
has a prototype, so there are default keys in the map. - (This can be bypassed using
map = Object.create(null)
.) - The keys of an Object are Strings or Symbols, where they can be of any value for a Map.
Use case
use as an advanced version of object (hash map) to store. eg. Store the DOM element as the keys;
1 | <body> |
1 | let map =new Map(); |
1 | I accept aggreement: |
1 | function post() { |
Weak Map
The 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.
- Similar API with Map
WeakMap
keys are not enumerable (i.e., there is no method giving you a list of the keys). If they were, the list would depend on the state of garbage collection, introducing non-determinism.- they are a target of garbage collection (GC) if there is no other reference to the object anymore.
1 | let wm = new WeakMap() |
Use case
to store private data for an object, or to hide implementation details.
1 | const privates = new WeakMap(); |
Set
The Set object lets you store unique
values of any type, whether primitive values or object references.
- ‘unique’:
NaN
andundefined
can also be stored in a Set. - All
NaN
values are equated (i.e. NaN is considered the same as NaN, even though NaN !== NaN).
1 | let set = new Set(['a',2]) |
Loop:
iterator: since set only has keys so the set.values() return the keys as well. the entries() returns [[keys:keys]]
1 | set.keys() |
string type can also be transfered
1 | let text = 'India'; |
1 | let set= new Set('17323845'); |
Use case
- remove duplicate from an array
1 | let arr=[1, 2, 3, 2, 'b', 1, 'b'] |
- borrow API from Array
1 | let intersection = new Set([...set1].filter(x => set2.has(x))); |
Search history
1 | let obj = { |
WeakSet
WeakSet objects are collections of objects. All objects in a WeakSet’s collection are unique.
Key difference with set:
WeakSets
are collections of objects only.WeakSets
are not enumerable.
1 | var ws = new WeakSet(); |
Use case
Store DOM element, if the element was deleted from the DOM, the weakset lost the reference as well. No wasting memory.
1 | <body> |
1 | class Todo { |