Upload
abdurrachman-mappuji
View
94
Download
3
Embed Size (px)
Citation preview
1
Kyle Simpson@getify
http://getify.me
http://speakerdeck.com/getify
3
5
6
7
https://developer.mozilla.org/en-US/docs/JavaScript
https://github.com/rwldrn/idiomatic.js
http://www.ecma-international.org/ecma-262/5.1/
http://wiki.ecmascript.org/doku.php?id=harmony:proposals
Advanced JavaScriptThe “What you need to know” Parts
8
9
Scope, Closures• Nested Scope• Hoisting• this• Closure
Agenda10
Scope
Scope: where to look for things
11
Scope
JavaScript has function scope only*
12
Scope
13
Scope
14
Scope
15
Scope: function scope?
16
Scope: block scope?
17
Scope
lexical scope
dynamic scope
18
Scope
19
Scope: lexical
20
Scope: WAT!?
21
Scope: WAT!?
22
Function Scope
23
IIFE
http://benalman.com/news/2010/11/immediately-invoked-function-expression/
Function Scope
24
Block Scope
25
Block Scope
let (ES6+)
26
Block Scope: let
27
Block Scope: let
28
Block Scope: let
29
Block Scope: let
30
https://gist.github.com/getify/5285514
Block Scope: let
31
https://github.com/getify/let-er
Block Scope: let
32
https://github.com/getify/let-er
Scope
dynamic scope
33
Scope: dynamic!?!?
34
Scope
35
Quiz1. What type of scoping rule(s) does JavaScript have? Exceptions?2. What are the different ways you can create a new scope?3. What’s the difference between undeclared and undefined?
Scope: hoisting
36
Scope: hoisting
37
Scope: hoisting
38
Scope: hoisting
39
Scope: hoisting
40
Hoisting: functions first
41
Hoisting: recursion
42
Hoisting: let gotcha
43
(exercise #1: 10min)
44
this
45
this
46
Every function, while executing, has a reference to its current execution context,
called this.
this
47
Remember lexical scope vs. dynamic scope?
JavaScript’s version of “dynamic scope” is this.
Scope
48
this: implicit & default binding
49
this: implicit & default binding
50
this: binding confusion
51
this: binding confusion
52
this: explicit binding
53
this: hard binding
54
this: hard binding
55
this: hard binding
56
this: hard binding
57https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
this: new
58
this: new, order of precedence
59
this: determination
60
1. Was the function called with `new`?2. Was the function called with `call` or `apply` specifying an explicit this?3. Was the function called via a containing/owning object (context)?4. DEFAULT: global object (except strict mode)
this
61
Quiz1. What determines which object a function’s this points to? What’s the default?2. How do you “borrow” a function by implicit assignment of this?3. How do you explicitly bind this?4. How can you seal a specific this to a function? Why do that? Why not?5. How do you create a new this?
Closure
62
Closure
63
Remember lexical scope?
Closure absolutely depends on it. Go review it if you’re still
confused. :)
Closure
64
Closure is when a function “remembers” its lexical scope even when the function is
executed outside that lexical scope.
Closure
65
Closure
66
Closure
67
Closure
68
Closure: shared scope
69
Closure: nested scope
70
Closure: loops
71
Closure: loops
72
Closure: loops + block scope
73
Closure?
74
Closure: classic module pattern
75
Closure: modified module pattern
76
Closure: modern module pattern
77
Closure: future/ES6+ module pattern
78foo.js:
Closure
79
Quiz1. What is a closure and how is it created?2. How long does its scope stay around?3. Why doesn’t a function callback inside a loop behave as expected? How do we fix it?4. How do you use a closure to create an encapsulated module? What’s the benefits of that approach?
(exercise #2: 20min)
80
81
Object-Orienting• Common OO Patterns• prototype• “Inheritance” vs. “Behavior Delegation”
(OO vs. OLOO)
Agenda82
OO Design Patterns
83
OO Design Patterns
84
Singleton
OO Design Patterns: singleton
85
OO Design Patterns: singleton
86
OO Design Patterns
87
Observer
OO Design Patterns: observer
88
prototype
89
prototype
90
Every single “object” is built by a constructor
function
prototype
91
Each time a constructor is called, a new object is
created
prototype
92
A constructor makes an object “based on” its own
prototype
prototype
93
A constructor makes an object linked to its own
prototype
prototype
94
prototype
95
prototype
96
prototype
97
Scope
98
prototype: objects linked
99
prototype: objects linked
100
prototype: objects linked
101
prototype
102
Quiz1. What is a constructor?2. What is a [[Prototype]] and where does it come from?3. How does a [[Prototype]] affect an object?4. How do we find out where an object’s [[Prototype]] points to (3 ways)?
prototype: this redux
103
remember how this has a pesky way of getting
unassigned?
prototype: this redux
104
(exercise #3: 10min)
105
OO
106
Inheritance
OO: classical inheritance
107
Foo
Bar
a1
a2
b1
b2
OO: “prototypal inheritance”
108
Foo.prototype a1
a2
b1
b2
(another design pattern)
Bar.prototype
OO: js
109
JS “Inheritance”“Behavior Delegation”
OO: inheritance delegation
110
OO: behavior delegation
111
Let’s simplify!
OLOO
112
OLOO:Objects Linked to Other
Objects
OLOO: delegated objects
113
OLOO: delegated objects
114
OLOO: delegated objects
115
OLOO: delegated objects
116
117
OO: old & busted
OO: old & busted
118
119
OLOO: new hotness
120
OLOO: new hotness
OLOO: Object.create()
121
OLOO: object creation
122
https://gist.github.com/getify/5572383
https://gist.github.com/getify/5226305
OLOO: js
123
Quiz1. How is JavaScript’s [[Prototype]] chain not like traditional/classical inheritance?2. What does “behavior delegation” mean and how does it describe object linking in JS?3. Why is “behavior delegation” as a design pattern a helpful thing? What are the tradeoffs?
(exercise #4: 15min)
124
125
126
Async Patterns• Callbacks• Generators/Coroutines• Promises
Agenda
Async Patterns
127
Callbacks
Async Patterns: callbacks
128
Async Patterns: “callback hell”
129
Async Patterns: “callback hell”
130
Async Patterns: “callback hell”
131
Inversion of Control
Async Patterns: separate callbacks
132
Async Patterns: “error-first style”
133
Async Patterns: nested-callback tasks
134
Async Patterns
135
Generators (yield)
Async Patterns: generators
136
Async Patterns: generator coroutines
137
Async Patterns: generator messages
138
Async Patterns: yield tasks
139
Async Patterns
140
Promises
“continuation events”
Async Patterns: jQuery-style promises
141
Async Patterns: jQuery-style promises
142
Async Patterns: (native) promise tasks
143
Async Patterns: promises sequence
144
sequence = automatically chained promises
Async Patterns: sequences & gates
145https://github.com/getify/asynquence
Async Patterns: sequence tasks
146
Async Patterns: generator+sequence tasks
147
Async Patterns: CSP-style generators
148
Async Patterns
149
Quiz1. What is “callback hell”? Why do callbacks suffer from “inversion of control”?2. How do you pause a generator? How do you resume it?3. What is a Promise? How does it solve inversion of control issues?4. How do we combine generators and promises for flow control?
(exercise #5: 15min)
150
151