-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathES6 The Right Parts
104 lines (88 loc) · 2.45 KB
/
ES6 The Right Parts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Arrow Functions
Introduction
The Arrow Function
What is arrow function? How is it useful?
=> suppose to replace "function" keyword
foo = () => 2; // or foo = x => 2;
function foo() {
return 2;
}
but arrows can have variations - and this can be a problem
other than reability
Arrow Function Variations
() => 3;
x => 3;
_ => 3; // warning - Do not user - Can be confusing or cause error
Three things to do with parameters - all must be enclosed with parenthesis
1. default values
2. spread
3. destructuring
(...x) apply the gather operator on parameters
(x,y) => 3; // multiple parameters need to be enclosed in parenthesis
x => {try {3;} catch(e) {} } // now need to use "return" keyword to return value
x => {return 3;} // like this
x => ({y: x}) // return a JSON object
arrow function are anonymous
- cannot be recusive
- cannot reference self easily
- hard to debug with anonymous functions
Name inferencing
var foo = x => 3;
foo.name; // "foo"
...but it dos not work when arrow function is passed in as argument
foo( x => 3 )
Promises and This
p.then( function(v) {return v.id} )
p.then( v => v.id )
if v is null then exception will be thrown from anonymous function
- it will be difficult to debug through stack trace
A better way might be naming the function:
p.then( function extractId(v) {return v.id} )
// the code is more readable and easier to debug through stack trace
Exercise 0: The Arrow Function
Exercise 0 Solution
Block Scope
Let vs. Var
Closures and Explicit Blocks
Const
When to Use Const
Exercise 1: Variable Scoping
Exercise 1 Solution
Default Values and the Gather/Spread Operator
Default Values
Lazy Expressions
Gather and Spread Operators, Part 1
Gather and Spread Operators, Part 2
Using the Gather and Spread Operators
Exercise 2
Exercise 2 Solution
Babel
Destructuring
Audience Q&A: TypeScript vs. JavaScript
Array Destructuring
Destructuring and Default Values
Dumping Variables
Nested Array Destructuring
Object Destructuring
Nested Object Destructuring
Destructuring and Function Parameters
Advanced Destructuring
Exercise 3
Exercise 3 Solution
Template Strings
Concise Properties and Methods
Template Strings
Tag Functions
Exercise 4
Exercise 4 Solution
Symbols, Iterators, and Generators
Symbols
Well-known Symbols
Iterators
Creating a Custom Iterator
Generators
Computed Generator Methods
Exercise 5
Exercise 5 Solution
Ranges
Optimizing Code for the Reader