Lean Publishing - Reg Braithwaite - JavaScript Allongé [2014, PDF, ENG]
Год: 2014
Автор: Reg Braithwaite
Жанр: Программирование
Издательство: Lean Publishing
Серия: Leanpub
Язык: Английский
Формат: PDF
Качество: Изначально компьютерное (eBook)
Интерактивное оглавление: Да
Количество страниц: 250
Описание: A different kind of language requires a different kind of book.
JavaScript holds surprising depths–its scoping rules are neither strictly lexical nor strictly dynamic,
and it supports procedural, object-oriented (in several flavors!), and functional programming. Many
books try to hide most of those capabilities away, giving you recipes for writing JavaScript in a way
that approximates class-centric programming in other languages. Not JavaScript Allongé. It starts
with the fundamentals of values, functions, and objects, and then guides you through JavaScript
from the inside with exploratory bits of code that illustrate scoping, combinators, context, state,
prototypes, and constructors.
Like JavaScript itself, this book gives you a gentle start before showing you its full depth, and like a
Cafe Allongé, it’s over too soon. Enjoy!
Оглавление
Contents
A Pull of the Lever: Prefaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Foreword by Michael Fogus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Foreword by Matthew Knox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
Why JavaScript Allongй? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
A Personal Word About The Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Legend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Prelude: Values and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi
values and expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi
values and identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1 The first sip: Basic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
As Little As Possible About Functions, But No Less . . . . . . . . . . . . . . . . . . . . . . 3
Ah. I’d Like to Have an Argument, Please. . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Closures and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Let’s Talk Var . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Naming Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Combinators and Function Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
I’d Like to Have Some Arguments. Again. . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2 The Recipe Cheat Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3 Recipes with Basic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Partial Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Ellipses and improved Partial Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Unary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Tap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Maybe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4 The Pause That Refreshes: Rebinding and References . . . . . . . . . . . . . . . . . . . 50
Arguments and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
References and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Reassignment and Mutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
CONTENTS
How to Shoot Yourself in the Foot With Var . . . . . . . . . . . . . . . . . . . . . . . . . . 61
When Rebinding Meets Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
From Let to Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
5 Recipes with Rebinding and References . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
mapWith . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Flip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Extend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6 Stir the Allongй: Objects, Mutation, and State . . . . . . . . . . . . . . . . . . . . . . . 87
Encapsulating State with Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Composition and Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
This and That . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
What Context Applies When We Call a Function? . . . . . . . . . . . . . . . . . . . . . . 102
Method Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7 Recipes with Objects, Mutations, and State . . . . . . . . . . . . . . . . . . . . . . . . . 110
Memoize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
getWith . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
pluckWith . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Deep Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
8 Finish the Cup: Instances and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Prototypes are Simple, it’s the Explanations that are Hard To Understand . . . . . . . . . . 119
Binding Functions to Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Partial Application, Binding, and Currying . . . . . . . . . . . . . . . . . . . . . . . . . . 129
A Class By Any Other Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Extending Classes with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
9 Recipes with Instances and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Unbinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Send . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Fluent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Once Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
CONTENTS
10 Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Introduction: Compose and Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
11 New Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
How Prototypes and Constructors differ from Classes . . . . . . . . . . . . . . . . . . . . 164
New-Agnostic Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Another New-Agnostic Constructor Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Class Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Interlude: Tortoises, Hares, and Teleporting Turtles . . . . . . . . . . . . . . . . . . . . . . 176
Functional Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Refactoring to Functional Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
A Drunken Walk Across A Chequerboard . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Trampolining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
12 Recipes for New Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Before . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
After . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Provided and Except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
A Functional Mixin Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
A Class Decorator Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Iterator Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
The Golden Crema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Author’s Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
How to run the examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Thanks! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
CoffeeScript Ristretto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Copyright Notice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
About The Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231