www.it-ebooks.info www.it-ebooks.info THIRD EDITION Learning Javascript Ethan Brown Boston www.it-ebooks.info Learning JavaScript by Ethan Brown Copyright © 2015 O’Reilly Media. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( http://safaribooksonline.com ). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected] . Editor: Meg Foley Proofreader: FILL IN PROOFREADER Production Editor: FILL IN PRODUCTION EDI‐ Indexer: FILL IN INDEXER TOR Interior Designer: David Futato Copyeditor: FILL IN COPYEDITOR Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest September 2015: Third Edition Revision History for the Third Edition 2015-12-15: First Early Release See http://oreilly.com/catalog/errata.csp?isbn=9781491914915 for release details. While the publisher and the author(s) have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibil‐ ity for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-91491-5 [FILL IN] www.it-ebooks.info Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Your First Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Where To Start 20 The Tools 20 A Comment on Comments 22 Getting Started 23 The JavaScript Console 25 jQuery 26 Drawing Graphics Primitive 26 Automating Repetitive Tasks 28 Handling User Input 29 Hello World 31 2. JavaScript Development Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Writing ES6 Today 33 ES6 Features 34 Installing Git 35 The Terminal 35 Your Project Root 36 Version Control: git 36 Package Management: npm 39 The Transcompilers 41 Build Tools: Gulp and Grunt 42 Project Structure 43 Running Babel with Gulp 44 Linting 45 Recap 48 iii www.it-ebooks.info 3. Literals, Variables, Constants, and Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Variables and Constants 51 Identifier Names 53 Literals 54 Primitive Types and Objects 54 Numbers 56 Strings 57 Escaping 58 Special Characters 59 Template Strings 60 Multiline Strings 60 Numbers as Strings 61 Booleans 62 Symbols 62 Null and Undefined 63 Objects 63 Number, String, and Boolean Objects 66 Arrays 66 Trailing Commas in Objects and Arrays 68 Dates 68 Regular Expressions 69 Maps and Sets 69 Data Type Conversion 70 Converting to Numbers 70 Converting to String 71 Converting to Boolean 71 Conclusion 71 4. Control Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 A Control Flow Primer 73 While Loops 77 Block Statements 77 Whitespace 78 Helper Functions 79 if…else Statement 80 Do-While Loop 81 For Loop 82 If Statement 83 Putting it all Together 84 Control Flow Statements in JavaScript 85 Control Flow Exceptions 86 Chaining if-else Statements 86 iv | Table of Contents www.it-ebooks.info Metasyntax 87 Additional for Loop Patterns 88 switch Statements 89 for…in loop 92 for…of loop 93 Useful Control Flow Patterns 93 Using continue to Reduce Conditional Nesting 93 Using break or return to Avoid Unnecessary Computation 94 Using Value of Index after Loop Completion 95 Using Descending Indexes When Modifying Lists 95 Conclusion 96 5. Expressions and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Operators 99 Arithmetic Operators 99 Operator Precedence 102 Comparison Operators 103 Comparing Numbers 105 String Concatenation 106 Logical Operators 106 Truthy and Falsy Values 107 AND, OR, and NOT 107 Short-Circuit Evaluation 109 Logical Operators with Non-Boolean Operands 109 Conditional Operator 110 Comma Operator 111 Grouping Operator 111 Bitwise Operators 111 typeof Operator 113 void Operator 114 Assignment Operators 114 Destructuring Assignment 115 Object & Array Operators 117 Expressions in Template Strings 118 Expressions and Control Flow Patterns 118 Converting if…else Statements to Conditional Expressions 118 Converting if Statements to Short-Circuited Logical OR Expressions 118 Conclusion 119 6. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Return Values 122 Calling vs. Referencing 122 Table of Contents | v www.it-ebooks.info Function Arguments 123 Do Arguments Make the Function? 125 Destructuring Arguments 126 Default Arguments 127 Functions as Properties of Objects 127 The this Keyword 128 Function Expressions and Anonymous Functions 130 Arrow Notation 131 call, apply and bind 132 Conclusion 134 7. Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Scope vs. Existence 136 Lexical vs. Dynamic Scoping 136 Global Scope 137 Block Scope 139 Variable Masking 140 Functions, Closures, and Lexical Scope 141 Immediately-Invoked Function Expressions (IIFEs) 142 Function Scope and Hoisting 143 Function Hoisting 145 The Temporal Dead Zone 146 Strict Mode 146 Conclusion 147 8. Arrays and Array Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 A Review of Arrays 149 Array Content Manipulation 150 Adding or Removing Single Elements at the Beginning or End 151 Adding Multiple Elements at the End 151 Getting a Sub-Array 152 Adding or Removing Elements At any Position 152 Cutting and Replacing Within an Array 152 Filling an Array with a Specific Value 153 Reversing and Sorting Arrays 153 Array Searching 154 The Fundamental Array Operations: map and filter 156 Array Magic: reduce 158 Array Methods and Deleted or Never-Defined Elements 161 String Joining 161 Summary 162 vi | Table of Contents www.it-ebooks.info 9. Objects and Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Property Enumeration 165 for…in 166 Object.keys 166 Object-Oriented Programming 167 Class and Instance Creation 168 Dynamic Properties 169 Classes are Functions 170 The Prototype 171 Static Methods 173 Inheritance 174 Polymorphism 175 Enumerating Object Properties, Revisited 176 String Representation 177 Multiple Inheritance, Mixins, and Interfaces 177 Conclusion 179 10. Maps and Sets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Maps 181 Weak Maps 183 Sets 184 Weak Sets 185 Breaking the Object Habit 185 11. Exceptions and Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 The Error Object 187 Exception Handling with try and catch 188 Throwing Errors 189 Exception Handling and the Call Stack 189 try…catch…finally 191 Let Exceptions be Exceptional 192 12. Iterators and Generators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 The Iteration Protocol 195 Generators 197 yield Expressions and Two-Way Communication 198 Generators and return 201 Conclusion 201 13. Functions, and the Power of Abstract Thinking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Functions as Subroutines 203 Functions as Subroutines That Return a Value 204 Table of Contents | vii www.it-ebooks.info Functions as…Functions 205 So What? 207 Functions are Objects 208 IIFEs and Asynchronous Code 209 Function Variables 211 Functions in an Array 212 Pass a Function into a Function 214 Return a Function from a Function 215 Recursion 216 Conclusion 217 14. Asynchronous Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 The Analogy 220 Callbacks 220 setInterval and clearInterval 221 Scope and Asynchronous Execution 222 Error-First Callbacks 223 Callback Hell 224 Promises 225 Creating Promises 226 Using Promises 226 Events 228 Promise Chaining 230 Preventing Unsettled Promises 231 Generators 232 One Step Forward and Two Steps Back? 235 Don’t Write Your Own Generator Runner 236 Exception Handling in Generator Runners 236 Conclusion 237 15. Date and Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Dates, Time Zones, Timestamps, and the Unix Epoch 239 Constructing Date Objects 240 Moment.js 241 A Practical Approach to Dates in JavaScript 242 Constructing Dates 242 Constructing Dates on the Server 242 Constructing Dates in the Browser 243 Transmitting Dates 243 Displaying Dates 244 Date Components 245 Comparing Dates 246 viii | Table of Contents www.it-ebooks.info
Description: