Let’s review some JavaScript features in a hurry!
Iterables
Objects that can be used in for..of
are called iterable.
Iterables must implement the method named Symbol.iterator
. The result
of obj[Symbol.iterator]
is called an iterator. An iterator must have
the method named next()
that returns an object {done: Boolean, value:
any}
, the value is the next value. The Symbol.iterator
method is
called automatically by for..of
, but we also can do it directly.
Array.from(obj[, mapFn, thisArg])
makes a real Array of an iterable or
array-like obj, and we can then use array methods on it. The optional
arguments mapFn and thisArg allow us to apply a function to each item.
Destructuring
Map an object or array to variables.
Object syntax:
let {prop : varName = default, ...} = object
Array syntax:
let [item1 = default, item2, ...rest] = array
The first item goes to item1
; the second goes into item2
, all the rest
makes the array rest.
Map and Set
Map
, is a collection of keyed values, it allows objects to be keys and
provides a size
property.
Set
, is a collection of unique values, does not allow to reorder elements.
The following allow garbage-collection:
There ara also WeakMap
and WeakSet
which provide garbage collection,
I need to research more about these. Maybe the next post.