Table of Contents
Preface vii
1 Beautiful Mixins 1
Classical Inheritance 1
Prototypes 2
Mixins 3
The Basics 4
The Use Case 4
Classic Mixins 5
The extend Function 6
Functional Mixins 7
Adding Options 8
Adding Caching 9
Advice 10
Wrapup 11
2 Eval and Domain-Specific Languages 13
What About "eval is Evil"? 13
History and Interface 14
Performance 15
Common Uses 16
A Template Compiler 16
Speed 18
Mixing Languages 19
Dependencies and Scopes 20
Debugging Generated Code 21
Binary Pattern Matches 21
Closing Thoughts 25
3 How to Draw a Bunny 27
What Is a Rabbit? 27
What Is a Bunny? 28
What Does This Have to Do with JavaScript? 29
With So Much Variation, Which Way Is Correct? 32
How Does This Affect the Classroom? 33
Is This Art? And Why Does That Matter? 34
What Does This Look Like? 36
What Did I Just Read? 38
4 Too Much Rope, or JavaScript for Teams 39
Know Your Audience 40
Stupid Good 40
Keep It Classy 41
Style Rules 43
Evolution of Code 44
Conclusion 44
5 Hacking JavaScript Constructors for Model Harmony 47
Doppelgangers 48
Miniature Models of Factories 50
Constructor Identity Crisis 51
Making It Scale 52
Conclusion 54
6 One World, One Language 57
An Imperative, Dynamic Proposal 58
The Paradox of Choice 60
Globalcommunicationscript 60
7 Math Expression Parser and Evaluator 61
Lexical Analysis and Tokens 61
Syntax Parser and Syntax Tree 66
Tree Walker and Expression Evaluator 72
Final Words 76
8 Evolution 77
Backbone 79
New Possibilities 80
9 Error Handling 83
Assume Your Code Will Fail 83
Throwing Errors 84
When to Throw Errors 86
Types of Errors 86
Custom Errors 88
Handling Errors 89
Global Error Handling in Browsers 91
Global Error Handling in Node.js 92
Summary 93
10 The Node.js Event Loop 95
Event-Driven Programming 95
Asynchronous, Nonblocking I/O 97
Concurrency 99
Adding Tasks to the Event Loop 99
11 JavaScript Is 101
JavaScript Is Dynamic 101
JavaScript Can Be Static 102
JavaScript Is Functional 102
JavaScript Does Everything 103
12 Coding Beyond Logic 105
0 The Basement 105
1 Quine's Paradox 105
2 The Conjecture 110
3 Peer Review 112
13 JavaScript Is Cutieful 115
All This Loose Beauty 115
The Absurdity of Dalí 115
Dalí's JavaScript 116
Is This Beauty Just Ugly? 116
An Unfortunate Necessity 116
The Beauty Is in the Madness 116
Let's Have a Wee Look at map 116
Hello, thisArg 117
Okay! So That's a Bunch of Stuff I Already Knew About [].map-Now What? 117
Calling All Cars 117
Number 117
Now I Know Everything 118
Wild 118
14 Functional JavaScript 119
Functional Programming 119
Functional JavaScript 121
Objects 126
Now What? 127
15 Progress 129
Index 147