Array Destructuring
assignment expression get the whole thing that was subject to
assignment 1
2
3
4
5
6
7
8
9
10
11
12function data (){
return [1,2,3]
}
var tmp;
var o = [];
tmp = [
o[0],
] = data()
console.log(tmp). // [1,2,3]
console.log(o) //. [1]
comma separation
empty positions using comma 1
2
3
4
5
6var [
first,
,
third,
...others
] = data()
swap variables 1
[a,b]=[b,a]
parameter arrays
recommended way to have a default deconstructing fallback to prevent
type error
1
2
3
4
5
6
7
8function data([
first = 10,
second = 20,
third = 30,
] = []){
console.log(first) // 10
}
data(undefined)
nested array restructuring
recommended way to have a default deconstructing fallback to prevent
type error 1
2
3
4
5
6
7
8
9
10
11
12
13function data(){
return [1, undefined, 4]
}
var [
first,
[
second,
third,
] = [],
forth
] = data()
Iterators
an iterator is an object which defines a sequence and potentially a return value upon its termination.
Specifically, an iterator is any object which implements the Iterator protocol by having a next() method that returns an object with two properties:
value The next value in the iteration sequence.
done This is true if the last value in the sequence has already been consumed. If value is present alongside done, it is the iterator's return value.
1 | var it1 = "Hello" |
1 | var obj = { |
Generators
The Generator object is returned by a generator function and it
conforms to both the iterable protocol and the iterator protocol.
1
2
3
4
5
6
7
8
9
10
11
12var obj = {
a: 1,
b: 2,
c: 3,
*[Symbol.interator](){
for(let key of Object.keys(this)){
yield this[key];
}
}
}
[..obj] // [1,2,3]
Regular Expressions
Look Ahead
1 | var msg = "Hello Message" |
Look Behind
1 | // positive look behind |
Regex Excercise
A regex generator example
1 | var poem = ` |
output 1
2
3
4
5
6
7
8
9
10
11a gun: kill at power quokka.js:13:4
fire: burn at power quokka.js:13:4
wind: chill at power quokka.js:13:4
a mind: learn at power quokka.js:13:4
anger: rage at power quokka.js:13:4
a smile: heal at power quokka.js:13:4