logo资料库

Eloquent JavaScript 3rd Edition (True PDF).pdf

第1页 / 共448页
第2页 / 共448页
第3页 / 共448页
第4页 / 共448页
第5页 / 共448页
第6页 / 共448页
第7页 / 共448页
第8页 / 共448页
资料共448页,剩余部分请下载后查看
Introduction
On programming
Why language matters
What is JavaScript?
Code, and what to do with it
Overview of this book
Typographic conventions
Values, Types, and Operators
Values
Numbers
Strings
Unary operators
Boolean values
Empty values
Automatic type conversion
Summary
Program Structure
Expressions and statements
Bindings
Binding names
The environment
Functions
The console.log function
Return values
Control flow
Conditional execution
while and do loops
Indenting Code
for loops
Breaking Out of a Loop
Updating bindings succinctly
Dispatching on a value with switch
Capitalization
Comments
Summary
Exercises
Functions
Defining a function
Bindings and scopes
Functions as values
Declaration notation
Arrow functions
The call stack
Optional Arguments
Closure
Recursion
Growing functions
Functions and side effects
Summary
Exercises
Data Structures: Objects and Arrays
The weresquirrel
Data sets
Properties
Methods
Objects
Mutability
The lycanthrope's log
Computing correlation
Array loops
The final analysis
Further arrayology
Strings and their properties
Rest parameters
The Math object
Destructuring
JSON
Summary
Exercises
Higher-Order Functions
Abstraction
Abstracting repetition
Higher-order functions
Script data set
Filtering arrays
Transforming with map
Summarizing with reduce
Composability
Strings and character codes
Recognizing text
Summary
Exercises
The Secret Life of Objects
Encapsulation
Methods
Prototypes
Classes
Class notation
Overriding derived properties
Maps
Polymorphism
Symbols
The iterator interface
Getters, setters, and statics
Inheritance
The instanceof operator
Summary
Exercises
Project: A Robot
Meadowfield
The task
Persistent data
Simulation
The mail truck's route
Pathfinding
Exercises
Bugs and Errors
Language
Strict mode
Types
Testing
Debugging
Error propagation
Exceptions
Cleaning up after exceptions
Selective catching
Assertions
Summary
Exercises
Regular Expressions
Creating a regular expression
Testing for matches
Sets of characters
Repeating parts of a pattern
Grouping subexpressions
Matches and groups
The Date class
Word and string boundaries
Choice patterns
The mechanics of matching
Backtracking
The replace method
Greed
Dynamically creating RegExp objects
The search method
The lastIndex property
Parsing an INI file
International characters
Summary
Exercises
Modules
Modules
Packages
Improvised modules
Evaluating data as code
CommonJS
ECMAScript modules
Building and bundling
Module design
Summary
Exercises
Asynchronous Programming
Asynchronicity
Crow tech
Callbacks
Promises
Failure
Networks are hard
Collections of promises
Network flooding
Message routing
Async functions
Generators
The event loop
Asynchronous bugs
Summary
Exercises
Project: A Programming Language
Parsing
The evaluator
Special forms
The environment
Functions
Compilation
Cheating
Exercises
JavaScript and the Browser
Networks and the Internet
The Web
HTML
HTML and JavaScript
In the sandbox
Compatibility and the browser wars
The Document Object Model
Document structure
Trees
The standard
Moving through the tree
Finding elements
Changing the document
Creating nodes
Attributes
Layout
Styling
Cascading styles
Query selectors
Positioning and animating
Summary
Exercises
Handling Events
Event handlers
Events and DOM nodes
Event objects
Propagation
Default actions
Key events
Pointer events
Scroll events
Focus events
Load event
Events and the event loop
Timers
Debouncing
Summary
Exercises
Project: A Platform Game
The game
The technology
Levels
Reading a level
Actors
Encapsulation as a burden
Drawing
Motion and collision
Actor updates
Tracking keys
Running the game
Exercises
Drawing on Canvas
SVG
The canvas element
Lines and surfaces
Paths
Curves
Drawing a pie chart
Text
Images
Transformation
Storing and clearing transformations
Back to the game
Choosing a graphics interface
Summary
Exercises
HTTP and Forms
The protocol
Browsers and HTTP
Fetch
HTTP sandboxing
Appreciating HTTP
Security and HTTPS
Form fields
Focus
Disabled fields
The form as a whole
Text fields
Checkboxes and radio buttons
Select fields
File fields
Storing data client-side
Summary
Exercises
Project: A Pixel Art Editor
Components
The state
DOM building
The canvas
The application
Drawing tools
Saving and loading
Undo history
Let's draw
Why is this so hard?
Exercises
Node.js
Background
The node command
Modules
Installing with NPM
The file system module
The HTTP module
Streams
A file server
Summary
Exercises
Project: Skill-Sharing Website
Design
Long polling
HTTP interface
The server
The client
Exercises
Exercise Hints
Program Structure
Functions
Data Structures: Objects and Arrays
Higher-Order Functions
The Secret Life of Objects
Project: A Robot
Bugs and Errors
Regular Expressions
Modules
Asynchronous Programming
Project: A Programming Language
The Document Object Model
Handling Events
Project: A Platform Game
Drawing on Canvas
HTTP and Forms
Project: A Pixel Art Editor
Node.js
Project: Skill-Sharing Website
Eloquent JavaScript 3rd edition Marijn Haverbeke
Copyright © 2018 by Marijn Haverbeke This work is licensed under a Creative Commons attribution-noncommercial license (http://creativecommons.org/licenses/by-nc/3.0/). All code in the book may also be considered licensed under an MIT license (http://opensource. org/licenses/MIT). The illustrations are contributed by various artists: Cover and chapter illus- trations by Madalina Tantareanu. Pixel art in Chapters 7 and 16 by Antonio Perdomo Pastor. Regular expression diagrams in Chapter 9 generated with regexper.com by Jeff Avallone. Village photograph in Chapter 11 by Fabrice Creuzot. Game concept for Chapter 15 by Thomas Palef. The third edition of Eloquent JavaScript was made possible by 325 financial backers. You can buy a print version of this book, with an extra bonus chapter included, printed by No Starch Press at http://a-fwd.com/com=marijhaver-20&asin- com=1593279507. i
Contents Introduction On programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why language matters . . . . . . . . . . . . . . . . . . . . . . . . . . . What is JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Code, and what to do with it . . . . . . . . . . . . . . . . . . . . . . . Overview of this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . Typographic conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Values, Types, and Operators Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unary operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Empty values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Automatic type conversion . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Program Structure . . . . . . . . . . . . . . . . . . . . . . . . Expressions and statements Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binding names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The console.log function . . . . . . . . . . . . . . . . . . . . . . . . . . Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Control flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . while and do loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Indenting Code for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 3 6 7 8 8 10 10 11 13 15 16 18 18 20 22 22 23 25 25 26 26 27 27 28 30 31 32 33 ii
Updating bindings succinctly . . . . . . . . . . . . . . . . . . . . . . . Dispatching on a value with switch . . . . . . . . . . . . . . . . . . . . Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Functions Defining a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bindings and scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions as values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Declaration notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrow functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The call stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Growing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions and side effects . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Data Structures: Objects and Arrays The weresquirrel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The lycanthrope’s log . . . . . . . . . . . . . . . . . . . . . . . . . . . . Computing correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . Array loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The final analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Further arrayology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings and their properties . . . . . . . . . . . . . . . . . . . . . . . . Rest parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Math object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 34 35 36 37 37 39 39 40 42 43 44 45 46 47 49 51 54 55 55 57 57 58 59 60 61 63 64 66 68 68 70 72 74 75 76 77 78 iii
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Higher-Order Functions Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstracting repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . Higher-order functions . . . . . . . . . . . . . . . . . . . . . . . . . . . Script data set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Filtering arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transforming with map . . . . . . . . . . . . . . . . . . . . . . . . . . . Summarizing with reduce . . . . . . . . . . . . . . . . . . . . . . . . . . Composability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings and character codes . . . . . . . . . . . . . . . . . . . . . . . . Recognizing text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 82 83 83 85 86 87 88 88 90 91 93 95 95 6 The Secret Life of Objects 97 97 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Class notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Overriding derived properties . . . . . . . . . . . . . . . . . . . . . . . 103 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 The iterator interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Getters, setters, and statics . . . . . . . . . . . . . . . . . . . . . . . . 110 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 The instanceof operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 7 Project: A Robot 117 Meadowfield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 The task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Persistent data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 The mail truck’s route . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Pathfinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 iv
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 8 Bugs and Errors 128 Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Strict mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Error propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Cleaning up after exceptions . . . . . . . . . . . . . . . . . . . . . . . . 136 Selective catching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 9 Regular Expressions 143 Creating a regular expression . . . . . . . . . . . . . . . . . . . . . . . 143 Testing for matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Sets of characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Repeating parts of a pattern . . . . . . . . . . . . . . . . . . . . . . . . 146 Grouping subexpressions . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Matches and groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 The Date class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Word and string boundaries . . . . . . . . . . . . . . . . . . . . . . . . 150 Choice patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 The mechanics of matching . . . . . . . . . . . . . . . . . . . . . . . . 151 Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 The replace method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Greed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Dynamically creating RegExp objects . . . . . . . . . . . . . . . . . . 157 The search method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 The lastIndex property . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Parsing an INI file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 International characters . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 10 Modules 167 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 v
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Improvised modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Evaluating data as code . . . . . . . . . . . . . . . . . . . . . . . . . . 170 CommonJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 ECMAScript modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Building and bundling . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Module design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 11 Asynchronous Programming 180 Asynchronicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Crow tech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Networks are hard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Collections of promises . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Network flooding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Message routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Async functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 The event loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Asynchronous bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 12 Project: A Programming Language 202 Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 The evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Special forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 The environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Cheating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 13 JavaScript and the Browser 216 Networks and the Internet . . . . . . . . . . . . . . . . . . . . . . . . . 216 The Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 vi
分享到:
收藏