Business

Eloquent JavaScript A Modern Introduction to Programming

Description
Eloquent JavaScript A Modern Introduction to Programming
Categories
Published
of 489
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  Eloquent JavaScript A Modern Introduction to Programming Marijn Haverbeke  Copyright © 2014 by Marijn HaverbekeThis work is licensed under a Creative Commons attribution-noncommerciallicense ( http://creativecommons.org/licenses/by-nc/3.0/ ). All code in thebook may also be considered licensed under an MIT license ( http://opensource.org/licenses/MIT ).The illustrations are contributed by various artists: Cover by Wasif Hyder. Computer (introduction) and unicycle people (Chapter 21) byMax Xiantu. Sea of bits (Chapter 1) and weresquirrel (Chapter 4) byMargarita Martínez and José Menor. Octopuses (Chapter 2 and 4) byJim Tierney. Object with on/off switch (Chapter 6) by Dyle MacGregor.Regular expression diagrams in Chapter 9 generated with regexper.comby Jeff Avallone. Game concept for Chapter 15 by Thomas Palef. Pixelart in Chapter 16 by Antonio Perdomo Pastor.The second edition of Eloquent JavaScript was made possible by 454financial backers.You can buy a print version of this book, with an extra bonus chapterincluded, printed by No Starch Press at  http://www.amazon.com/gp/product/1593275846/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1593275846&linkCode=as2&tag=marijhaver-20&linkId=VPXXXSRYC5COG5R5 .i  Contents On programming . . . . . . . . . . . . . . . . . . . . . . . . . . 2Why language matters . . . . . . . . . . . . . . . . . . . . . . . 4What is JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . 6Code, and what to do with it . . . . . . . . . . . . . . . . . . . 8Typographic conventions . . . . . . . . . . . . . . . . . . . . . . 9 1 Values, Types, and Operators 10 Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Unary operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Undefined values . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Automatic type conversion . . . . . . . . . . . . . . . . . . . . . 18Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 Program Structure 22 Expressions and statements . . . . . . . . . . . . . . . . . . . . 22Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Keywords and reserved words . . . . . . . . . . . . . . . . . . . 25The environment . . . . . . . . . . . . . . . . . . . . . . . . . . 26Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26The console.log function . . . . . . . . . . . . . . . . . . . . . . 27Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27prompt and confirm . . . . . . . . . . . . . . . . . . . . . . . . . 28Control flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . 29while and do loops . . . . . . . . . . . . . . . . . . . . . . . . . . 31Indenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 33for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34ii  Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . 35Updating variables succinctly . . . . . . . . . . . . . . . . . . . 35Dispatching on a value with switch . . . . . . . . . . . . . . . . 36Capitalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3 Functions 41 Defining a function . . . . . . . . . . . . . . . . . . . . . . . . . 41Parameters and scopes . . . . . . . . . . . . . . . . . . . . . . . 42Nested scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Functions as values . . . . . . . . . . . . . . . . . . . . . . . . . 45Declaration notation . . . . . . . . . . . . . . . . . . . . . . . . 46The call stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 48Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Growing functions . . . . . . . . . . . . . . . . . . . . . . . . . . 54Functions and side effects . . . . . . . . . . . . . . . . . . . . . 57Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4 Data Structures: Objects and Arrays 60 The weresquirrel . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Data sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67The lycanthrope’s log . . . . . . . . . . . . . . . . . . . . . . . . 68Computing correlation . . . . . . . . . . . . . . . . . . . . . . . 70Objects as maps . . . . . . . . . . . . . . . . . . . . . . . . . . . 72The final analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 73Further arrayology . . . . . . . . . . . . . . . . . . . . . . . . . . 75Strings and their properties . . . . . . . . . . . . . . . . . . . . 77The arguments object . . . . . . . . . . . . . . . . . . . . . . . . 78iii  The Math object . . . . . . . . . . . . . . . . . . . . . . . . . . . 79The global object . . . . . . . . . . . . . . . . . . . . . . . . . . 81Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 5 Higher-Order Functions 85 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Abstracting array traversal . . . . . . . . . . . . . . . . . . . . . 87Higher-order functions . . . . . . . . . . . . . . . . . . . . . . . 89Passing along arguments . . . . . . . . . . . . . . . . . . . . . . 90JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Filtering an array . . . . . . . . . . . . . . . . . . . . . . . . . . 93Transforming with map . . . . . . . . . . . . . . . . . . . . . . . 94Summarizing with reduce . . . . . . . . . . . . . . . . . . . . . . 94Composability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95The cost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97Great-great-great-great-… . . . . . . . . . . . . . . . . . . . . . . 98Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 6 The Secret Life of Objects 104 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Overriding derived properties . . . . . . . . . . . . . . . . . . . 109Prototype interference . . . . . . . . . . . . . . . . . . . . . . . 111Prototype-less objects . . . . . . . . . . . . . . . . . . . . . . . . 113Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Laying out a table . . . . . . . . . . . . . . . . . . . . . . . . . . 114Getters and setters . . . . . . . . . . . . . . . . . . . . . . . . . 120Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121The instanceof operator . . . . . . . . . . . . . . . . . . . . . . . 123Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125iv
Search
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x