ebook img

Complete JavaScript Secrets & Tips for Professionals PDF

416 Pages·2017·3.66 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Complete JavaScript Secrets & Tips for Professionals

JavaScript Complete Complete Tips & Secrets for Professionals JavaScript ™ Tips & Secrets for Professionals 300+ pages of professional hints and tricks Disclaimer GoalKicker.com This is an unocial free book created for educational purposes and is not aliated with ocial JavaScript™ group(s) or company(s). Free Programming Books All trademarks and registered trademarks are the property of their respective owners Contents About ................................................................................................................................................................................... 1 Chapter 1: Getting started with JavaScript ..................................................................................................... 2 Section 1.1: Using console.log() ..................................................................................................................................... 2 Section 1.2: Using the DOM API .................................................................................................................................... 4 Section 1.3: Using window.alert() .................................................................................................................................. 5 Section 1.4: Using window.prompt() ............................................................................................................................. 6 Section 1.5: Using window.confirm() ............................................................................................................................ 7 Section 1.6: Using the DOM API (with graphical text: Canvas, SVG, or image file) ................................................. 8 Chapter 2: Functions ................................................................................................................................................... 9 Section 2.1: Function Scoping ..................................................................................................................................... 10 Section 2.2: Currying ................................................................................................................................................... 11 Section 2.3: Immediately Invoked Function Expressions ........................................................................................ 12 Section 2.4: Named Functions ................................................................................................................................... 12 Section 2.5: Binding `this` and arguments ................................................................................................................. 15 Section 2.6: Functions with an Unknown Number of Arguments (variadic functions) ........................................ 17 Section 2.7: Anonymous Function ............................................................................................................................. 18 Section 2.8: Default parameters ................................................................................................................................ 20 Section 2.9: Call and apply ......................................................................................................................................... 22 Section 2.10: Partial Application ................................................................................................................................. 23 Section 2.11: Passing arguments by reference or value .......................................................................................... 23 Section 2.12: Function Arguments, "arguments" object, rest and spread parameters ........................................ 24 Section 2.13: Function Composition ........................................................................................................................... 25 Section 2.14: Get the name of a function object ...................................................................................................... 26 Section 2.15: Recursive Function ................................................................................................................................ 26 Section 2.16: Using the Return Statement ................................................................................................................. 27 Section 2.17: Functions as a variable ......................................................................................................................... 28 Chapter 3: Arrays ........................................................................................................................................................ 30 Section 3.1: Converting Array-like Objects to Arrays ............................................................................................... 30 Section 3.2: Reducing values ...................................................................................................................................... 32 Section 3.3: Mapping values ....................................................................................................................................... 34 Section 3.4: Filtering Object Arrays ........................................................................................................................... 34 Section 3.5: Sorting Arrays ......................................................................................................................................... 36 Section 3.6: Iteration .................................................................................................................................................... 38 Section 3.7: Destructuring an array ........................................................................................................................... 41 Section 3.8: Removing duplicate elements ............................................................................................................... 42 Section 3.9: Array comparison ................................................................................................................................... 42 Section 3.10: Reversing arrays ................................................................................................................................... 43 Section 3.11: Shallow cloning an array ....................................................................................................................... 44 Section 3.12: Concatenating Arrays ........................................................................................................................... 44 Section 3.13: Merge two array as key value pair ..................................................................................................... 46 Section 3.14: Array spread / rest ............................................................................................................................... 46 Section 3.15: Filtering values ....................................................................................................................................... 47 Section 3.16: Searching an Array ............................................................................................................................... 48 Section 3.17: Convert a String to an Array ................................................................................................................ 48 Section 3.18: Removing items from an array ........................................................................................................... 49 Section 3.19: Removing all elements ......................................................................................................................... 50 Section 3.20: Finding the minimum or maximum element ..................................................................................... 51 Section 3.21: Standard array initialization ................................................................................................................. 52 Section 3.22: Joining array elements in a string ...................................................................................................... 53 Section 3.23: Removing/Adding elements using splice() ........................................................................................ 53 Section 3.24: The entries() method ........................................................................................................................... 53 Section 3.25: Remove value from array ................................................................................................................... 54 Section 3.26: Flattening Arrays .................................................................................................................................. 54 Section 3.27: Append / Prepend items to Array ...................................................................................................... 55 Section 3.28: Object keys and values to array ........................................................................................................ 55 Section 3.29: Logical connective of values ............................................................................................................... 55 Section 3.30: Checking if an object is an Array ........................................................................................................ 56 Section 3.31: Insert an item into an array at a specific index .................................................................................. 56 Section 3.32: Sorting multidimensional array ........................................................................................................... 57 Section 3.33: Test all array items for equality .......................................................................................................... 58 Section 3.34: Copy part of an Array .......................................................................................................................... 58 Chapter 4: Objects ..................................................................................................................................................... 58 Section 4.1: Shallow cloning ........................................................................................................................................ 59 Section 4.2: Object.freeze ........................................................................................................................................... 59 Section 4.3: Object cloning ......................................................................................................................................... 60 Section 4.4: Object properties iteration .................................................................................................................... 61 Section 4.5: Object.assign ........................................................................................................................................... 62 Section 4.6: Object rest/spread (...) ........................................................................................................................... 63 Section 4.7: Object.defineProperty ............................................................................................................................ 63 Section 4.8: Accesor properties (get and set) .......................................................................................................... 64 Section 4.9: Dynamic / variable property names .................................................................................................... 64 Section 4.10: Arrays are Objects ................................................................................................................................ 65 Section 4.11: Object.seal .............................................................................................................................................. 66 Section 4.12: Convert object's values to array ......................................................................................................... 66 Section 4.13: Retrieving properties from an object .................................................................................................. 67 Section 4.14: Read-Only property .............................................................................................................................. 69 Section 4.15: Non enumerable property ................................................................................................................... 69 Section 4.16: Lock property description .................................................................................................................... 70 Section 4.17: Object.getOwnPropertyDescriptor ...................................................................................................... 70 Section 4.18: Descriptors and Named Properties ..................................................................................................... 71 Section 4.19: Object.keys ............................................................................................................................................. 72 Section 4.20: Properties with special characters or reserved words ..................................................................... 73 Section 4.21: Creating an Iterable object .................................................................................................................. 73 Section 4.22: Iterating over Object entries - Object.entries() ................................................................................. 74 Section 4.23: Object.values() ...................................................................................................................................... 74 Chapter 5: AJAX ........................................................................................................................................................... 74 Section 5.1: Sending and Receiving JSON Data via POST ...................................................................................... 75 Section 5.2: Add an AJAX preloader ......................................................................................................................... 75 Section 5.3: Displaying the top JavaScript questions of the month from Stack Overflow's API ........................ 76 Section 5.4: Using GET with parameters ................................................................................................................... 76 Section 5.5: Check if a file exists via a HEAD request .............................................................................................. 77 Section 5.6: Using GET and no parameters .............................................................................................................. 78 Section 5.7: Listening to AJAX events at a global level ........................................................................................... 78 Chapter 6: Classes ...................................................................................................................................................... 78 Section 6.1: Class Constructor .................................................................................................................................... 78 Section 6.2: Class Inheritance .................................................................................................................................... 79 Section 6.3: Static Methods ......................................................................................................................................... 79 Section 6.4: Getters and Setters ................................................................................................................................. 80 Section 6.5: Private Members ..................................................................................................................................... 81 Section 6.6: Methods ................................................................................................................................................... 81 Section 6.7: Dynamic Method Names ....................................................................................................................... 82 Section 6.8: Managing Private Data with Classes .................................................................................................... 82 Section 6.9: Class Name binding ............................................................................................................................... 84 Chapter 7: Arithmetic (Math) ................................................................................................................................ 85 Section 7.1: Constants ................................................................................................................................................. 85 Section 7.2: Remainder / Modulus (%) ...................................................................................................................... 85 Section 7.3: Rounding .................................................................................................................................................. 86 Section 7.4: Trigonometry .......................................................................................................................................... 88 Section 7.5: Bitwise operators .................................................................................................................................... 89 Section 7.6: Incrementing (++) ................................................................................................................................... 90 Section 7.7: Exponentiation (Math.pow() or **) ......................................................................................................... 91 Section 7.8: Random Integers and Floats ................................................................................................................. 91 Section 7.9: Addition (+) .............................................................................................................................................. 92 Section 7.10: Little / Big endian for typed arrays when using bitwise operators ................................................. 93 Section 7.11: Get Random Between Two Numbers .................................................................................................. 94 Section 7.12: Simulating events with dierent probabilities .................................................................................... 94 Section 7.13: Subtraction (-) ........................................................................................................................................ 95 Section 7.14: Multiplication (*) ..................................................................................................................................... 95 Section 7.15: Getting maximum and minimum ......................................................................................................... 96 Section 7.16: Restrict Number to Min/Max Range ................................................................................................... 96 Section 7.17: Ceiling and Floor .................................................................................................................................... 96 Section 7.18: Getting roots of a number .................................................................................................................... 97 Section 7.19: Random with gaussian distribution ..................................................................................................... 97 Section 7.20: Math.atan2 to find direction ................................................................................................................ 98 Section 7.21: Sin & Cos to create a vector given direction & distance ................................................................... 98 Section 7.22: Math.hypot ............................................................................................................................................ 99 Section 7.23: Periodic functions using Math.sin ........................................................................................................ 99 Section 7.24: Division (/) ........................................................................................................................................... 101 Section 7.25: Decrementing (--) .............................................................................................................................. 101 Chapter 8: Comparison Operations ................................................................................................................ 101 Section 8.1: Abstract equality / inequality and type conversion .......................................................................... 101 Section 8.2: NaN Property of the Global Object .................................................................................................... 103 Section 8.3: Short-circuiting in boolean operators ................................................................................................ 104 Section 8.4: Null and Undefined .............................................................................................................................. 106 Section 8.5: Abstract Equality (==) ........................................................................................................................... 107 Section 8.6: Logic Operators with Booleans ........................................................................................................... 108 Section 8.7: Automatic Type Conversions .............................................................................................................. 108 Section 8.8: Logic Operators with Non-boolean values (boolean coercion) ...................................................... 108 Section 8.9: Empty Array .......................................................................................................................................... 109 Section 8.10: Equality comparison operations ....................................................................................................... 109 Section 8.11: Relational operators (<, <=, >, >=) ....................................................................................................... 111 Section 8.12: Inequality ............................................................................................................................................. 112 Section 8.13: List of Comparison Operators ........................................................................................................... 113 Section 8.14: Grouping multiple logic statements .................................................................................................. 113 Section 8.15: Bit fields to optimise comparison of multi state data ..................................................................... 113 Chapter 9: Conditions ............................................................................................................................................. 114 Section 9.1: Ternary operators ................................................................................................................................. 115 Section 9.2: Switch statement .................................................................................................................................. 117 Section 9.3: If / Else If / Else Control ....................................................................................................................... 118 Section 9.4: Strategy ................................................................................................................................................. 120 Section 9.5: Using || and && short circuiting ........................................................................................................... 120 Chapter 10: Loops ..................................................................................................................................................... 121 Section 10.1: Standard "for" loops ............................................................................................................................ 121 Section 10.2: "for ... of" loop ...................................................................................................................................... 122 Section 10.3: "for ... in" loop ....................................................................................................................................... 123 Section 10.4: "while" Loops ....................................................................................................................................... 124 Section 10.5: "continue" a loop ................................................................................................................................. 125 Section 10.6: Break specific nested loops ............................................................................................................... 125 Section 10.7: "do ... while" loop .................................................................................................................................. 126 Section 10.8: Break and continue labels ................................................................................................................. 126 Chapter 11: Promises ............................................................................................................................................... 126 Section 11.1: Introduction ........................................................................................................................................... 126 Section 11.2: Promise chaining .................................................................................................................................. 128 Section 11.3: Waiting for multiple concurrent promises ......................................................................................... 129 Section 11.4: Reduce an array to chained promises .............................................................................................. 130 Section 11.5: Waiting for the first of multiple concurrent promises ...................................................................... 131 Section 11.6: "Promisifying" functions with callbacks ............................................................................................. 131 Section 11.7: Error Handling ...................................................................................................................................... 132 Section 11.8: Reconciling synchronous and asynchronous operations ................................................................ 135 Section 11.9: Delay function call ............................................................................................................................... 136 Section 11.10: "Promisifying" values ......................................................................................................................... 136 Section 11.11: Using ES2017 async/await .................................................................................................................. 137 Section 11.12: Performing cleanup with finally() ..................................................................................................... 137 Section 11.13: forEach with promises ........................................................................................................................ 138 Section 11.14: Asynchronous API request ................................................................................................................. 139 Chapter 12: Regular expressions ...................................................................................................................... 139 Section 12.1: Creating a RegExp Object ................................................................................................................... 139 Section 12.2: RegExp Flags ....................................................................................................................................... 140 Section 12.3: Check if string contains pattern using .test() ................................................................................... 140 Section 12.4: Matching With .exec() ......................................................................................................................... 140 Section 12.5: Using RegExp With Strings ................................................................................................................. 141 Section 12.6: RegExp Groups .................................................................................................................................... 142 Section 12.7: Replacing string match with a callback function ............................................................................. 143 Section 12.8: Using Regex.exec() with parentheses regex to extract matches of a string ................................ 143 Chapter 13: Date ........................................................................................................................................................ 144 Section 13.1: Create a new Date object ................................................................................................................... 144 Section 13.2: Convert to a string format ................................................................................................................. 146 Section 13.3: Creating a Date from UTC ................................................................................................................. 147 Section 13.4: Formatting a JavaScript date ............................................................................................................ 149 Section 13.5: Get the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC .............................. 151 Section 13.6: Get the current time and date ........................................................................................................... 151 Section 13.7: Increment a Date Object .................................................................................................................... 152 Section 13.8: Convert to JSON ................................................................................................................................. 153 Chapter 14: Error Handling .................................................................................................................................. 153 Section 14.1: Error objects ......................................................................................................................................... 153 Section 14.2: Interaction with Promises ................................................................................................................... 154 Section 14.3: Error types ........................................................................................................................................... 154 Section 14.4: Order of operations plus advanced thoughts .................................................................................. 155 Chapter 15: Geolocation ........................................................................................................................................ 157 Section 15.1: Get updates when a user's location changes ................................................................................... 157 Section 15.2: Get a user's latitude and longitude ................................................................................................... 158 Section 15.3: More descriptive error codes ............................................................................................................. 158 Chapter 16: Cookies ................................................................................................................................................. 158 Section 16.1: Test if cookies are enabled ................................................................................................................. 158 Section 16.2: Adding and Setting Cookies ............................................................................................................... 159 Section 16.3: Reading cookies .................................................................................................................................. 159 Section 16.4: Removing cookies ............................................................................................................................... 159 Chapter 17: Intervals and Timeouts ................................................................................................................ 159 Section 17.1: Recursive setTimeout .......................................................................................................................... 159 Section 17.2: Intervals ................................................................................................................................................ 160 Section 17.3: Intervals ................................................................................................................................................ 160 Section 17.4: Removing intervals ............................................................................................................................. 160 Section 17.5: Removing timeouts ............................................................................................................................. 161 Section 17.6: setTimeout, order of operations, clearTimeout ............................................................................... 161 Chapter 18: Generators ......................................................................................................................................... 162 Section 18.1: Generator Functions ............................................................................................................................ 162 Section 18.2: Sending Values to Generator ............................................................................................................. 163 Section 18.3: Delegating to other Generator .......................................................................................................... 164 Section 18.4: Iteration ................................................................................................................................................ 164 Section 18.5: Async flow with generators ................................................................................................................ 165 Section 18.6: Iterator-Observer interface ................................................................................................................ 165 Chapter 19: History .................................................................................................................................................. 167 Section 19.1: history.pushState() ............................................................................................................................... 167 Section 19.2: history.replaceState() ......................................................................................................................... 167 Section 19.3: Load a specific URL from the history list .......................................................................................... 168 Chapter 20: Strict mode ........................................................................................................................................ 168 Section 20.1: For entire scripts ................................................................................................................................. 168 Section 20.2: For functions ....................................................................................................................................... 168 Section 20.3: Changes to properties ....................................................................................................................... 169 Section 20.4: Changes to global properties ........................................................................................................... 169 Section 20.5: Duplicate Parameters ........................................................................................................................ 170 Section 20.6: Function scoping in strict mode ........................................................................................................ 170 Section 20.7: Behaviour of a function's arguments list ......................................................................................... 170 Section 20.8: Non-Simple parameter lists .............................................................................................................. 172 Chapter 21: Custom Elements ............................................................................................................................. 172 Section 21.1: Extending Native Elements ................................................................................................................. 172 Section 21.2: Registering New Elements ................................................................................................................. 173 Chapter 22: JSON ..................................................................................................................................................... 173 Section 22.1: JSON versus JavaScript literals ........................................................................................................ 173 Section 22.2: Parsing with a reviver function ......................................................................................................... 175 Section 22.3: Serializing a value ............................................................................................................................... 176 Section 22.4: Serializing and restoring class instances ......................................................................................... 177 Section 22.5: Serializing with a replacer function .................................................................................................. 178 Section 22.6: Parsing a simple JSON string ........................................................................................................... 178 Section 22.7: Cyclic object values ............................................................................................................................ 178 Chapter 23: Binary Data ....................................................................................................................................... 179 Section 23.1: Getting binary representation of an image file ............................................................................... 179 Section 23.2: Converting between Blobs and ArrayBuers ................................................................................. 179 Section 23.3: Manipulating ArrayBuers with DataViews ..................................................................................... 180 Section 23.4: Creating a TypedArray from a Base64 string ................................................................................. 180 Section 23.5: Using TypedArrays ............................................................................................................................ 181 Section 23.6: Iterating through an arrayBuer ...................................................................................................... 181 Chapter 24: Template Literals .......................................................................................................................... 182 Section 24.1: Basic interpolation and multiline strings ........................................................................................... 182 Section 24.2: Tagged strings ................................................................................................................................... 183 Section 24.3: Raw strings .......................................................................................................................................... 183 Section 24.4: Templating HTML With Template Strings ....................................................................................... 183 Section 24.5: Introduction ......................................................................................................................................... 184 Chapter 25: Web Storage ..................................................................................................................................... 185 Section 25.1: Using localStorage .............................................................................................................................. 185 Section 25.2: Simpler way of handling Storage ..................................................................................................... 186 Section 25.3: Storage events .................................................................................................................................... 186 Section 25.4: sessionStorage ................................................................................................................................... 187 Section 25.5: localStorage length ............................................................................................................................ 188 Section 25.6: Error conditions .................................................................................................................................. 188 Section 25.7: Clearing storage ................................................................................................................................. 188 Section 25.8: Remove Storage Item ........................................................................................................................ 188 Chapter 26: Fetch ..................................................................................................................................................... 189 Section 26.1: Getting JSON data .............................................................................................................................. 189 Section 26.2: Set Request Headers ......................................................................................................................... 189 Section 26.3: POST Data ........................................................................................................................................... 189 Section 26.4: Send cookies ....................................................................................................................................... 189 Section 26.5: GlobalFetch ......................................................................................................................................... 190 Section 26.6: Using Fetch to Display Questions from the Stack Overflow API ................................................... 190 Chapter 27: Scope .................................................................................................................................................... 190 Section 27.1: Closures ................................................................................................................................................ 190 Section 27.2: Hoisting ................................................................................................................................................ 192 Section 27.3: Dierence between var and let ........................................................................................................ 194 Section 27.4: Apply and Call syntax and invocation. ............................................................................................. 196 Section 27.5: Arrow function invocation ................................................................................................................. 197 Section 27.6: Bound invocation ................................................................................................................................ 198 Section 27.7: Method invocation .............................................................................................................................. 198 Section 27.8: Anonymous invocation ...................................................................................................................... 199 Section 27.9: Constructor invocation ...................................................................................................................... 199 Section 27.10: Using let in loops instead of var (click handlers example) ........................................................... 199 Chapter 28: Modules ............................................................................................................................................... 200 Section 28.1: Defining a module ............................................................................................................................... 200 Section 28.2: Default exports ................................................................................................................................... 201 Section 28.3: Importing named members from another module ........................................................................ 201 Section 28.4: Importing an entire module .............................................................................................................. 201 Section 28.5: Importing named members with aliases ......................................................................................... 202 Section 28.6: Importing with side eects ................................................................................................................ 202 Section 28.7: Exporting multiple named members ................................................................................................ 202 Chapter 29: Screen .................................................................................................................................................. 203 Section 29.1: Getting the screen resolution ............................................................................................................. 203 Section 29.2: Getting the “available” area of the screen ...................................................................................... 203 Section 29.3: Page width and height ....................................................................................................................... 203 Section 29.4: Window innerWidth and innerHeight Properties ............................................................................ 203 Section 29.5: Getting color information about the screen .................................................................................... 203 Chapter 30: Inheritance ......................................................................................................................................... 203 Section 30.1: Standard function prototype ............................................................................................................. 203 Section 30.2: Dierence between Object.key and Object.prototype.key ............................................................ 204 Section 30.3: Prototypal inheritance ....................................................................................................................... 204 Section 30.4: Pseudo-classical inheritance ............................................................................................................. 205 Section 30.5: Setting an Object's prototype ........................................................................................................... 206 Chapter 31: Timestamps ........................................................................................................................................ 207 Section 31.1: High-resolution timestamps ................................................................................................................ 207 Section 31.2: Get Timestamp in Seconds ................................................................................................................ 207 Section 31.3: Low-resolution timestamps ................................................................................................................ 207 Section 31.4: Support for legacy browsers ............................................................................................................. 207 Chapter 32: Destructuring assignment ......................................................................................................... 208 Section 32.1: Destructuring Objects ......................................................................................................................... 208 Section 32.2: Destructuring function arguments ................................................................................................... 209 Section 32.3: Nested Destructuring ......................................................................................................................... 209 Section 32.4: Destructuring Arrays .......................................................................................................................... 210 Section 32.5: Destructuring inside variables ........................................................................................................... 210 Section 32.6: Default Value While Destructuring ................................................................................................... 211 Section 32.7: Renaming Variables While Destructuring ........................................................................................ 211 Chapter 33: Workers ............................................................................................................................................... 211 Section 33.1: Web Worker ......................................................................................................................................... 211 Section 33.2: A simple service worker ..................................................................................................................... 212 Section 33.3: Register a service worker .................................................................................................................. 212 Section 33.4: Communicating with a Web Worker ................................................................................................ 213 Section 33.5: Terminate a worker ............................................................................................................................ 213 Section 33.6: Populating your cache ....................................................................................................................... 214 Section 33.7: Dedicated Workers and Shared Workers ........................................................................................ 214 Chapter 34: Variable coercion/conversion ................................................................................................ 215 Section 34.1: Double Negation (!!x) .......................................................................................................................... 215 Section 34.2: Implicit conversion .............................................................................................................................. 216 Section 34.3: Converting to boolean ....................................................................................................................... 216 Section 34.4: Converting a string to a number ...................................................................................................... 217 Section 34.5: Converting a number to a string ...................................................................................................... 217 Section 34.6: Primitive to Primitive conversion table ............................................................................................. 217 Section 34.7: Convert an array to a string .............................................................................................................. 218 Section 34.8: Array to String using array methods ............................................................................................... 218 Section 34.9: Converting a number to a boolean .................................................................................................. 218 Section 34.10: Converting a string to a boolean .................................................................................................... 218 Section 34.11: Integer to Float ................................................................................................................................... 219 Section 34.12: Float to Integer .................................................................................................................................. 219 Section 34.13: Convert string to float ....................................................................................................................... 219 Chapter 35: Debugging .......................................................................................................................................... 219 Section 35.1: Interactive interpreter variables ........................................................................................................ 219 Section 35.2: Breakpoints ......................................................................................................................................... 220 Section 35.3: Using setters and getters to find what changed a property ......................................................... 221 Section 35.4: Using the console ............................................................................................................................... 222 Section 35.5: Automatically pausing execution ...................................................................................................... 222 Section 35.6: Elements inspector ............................................................................................................................. 223 Section 35.7: Break when a function is called ........................................................................................................ 223 Section 35.8: Stepping through code ...................................................................................................................... 223 Chapter 36: Notifications API .............................................................................................................................. 224 Section 36.1: Requesting Permission to send notifications .................................................................................... 224 Section 36.2: Sending Notifications ......................................................................................................................... 224 Section 36.3: Closing a notification .......................................................................................................................... 224 Section 36.4: Notification events .............................................................................................................................. 225 Chapter 37: Built-in Constants ........................................................................................................................... 225 Section 37.1: null ......................................................................................................................................................... 225 Section 37.2: Testing for NaN using isNaN() .......................................................................................................... 226 Section 37.3: NaN ...................................................................................................................................................... 227 Section 37.4: undefined and null .............................................................................................................................. 227 Section 37.5: Infinity and -Infinity ............................................................................................................................ 228 Section 37.6: Number constants .............................................................................................................................. 229 Section 37.7: Operations that return NaN ............................................................................................................... 229 Section 37.8: Math library functions that return NaN ............................................................................................ 229 Chapter 38: WebSockets ....................................................................................................................................... 230 Section 38.1: Working with string messages ........................................................................................................... 230 Section 38.2: Establish a web socket connection ................................................................................................... 230 Section 38.3: Working with binary messages ......................................................................................................... 230 Section 38.4: Making a secure web socket connection ......................................................................................... 231 Chapter 39: Web Cryptography API ................................................................................................................ 231 Section 39.1: Creating digests (e.g. SHA-256) ......................................................................................................... 231 Section 39.2: Cryptographically random data ....................................................................................................... 232 Section 39.3: Generating RSA key pair and converting to PEM format .............................................................. 232 Section 39.4: Converting PEM key pair to CryptoKey ........................................................................................... 233 Chapter 40: Async functions (async/await) .............................................................................................. 234 Section 40.1: Introduction ......................................................................................................................................... 234 Section 40.2: Await and operator precedence ...................................................................................................... 235 Section 40.3: Async functions compared to Promises .......................................................................................... 235 Section 40.4: Looping with async await .................................................................................................................. 236 Section 40.5: Less indentation ................................................................................................................................. 238 Section 40.6: Simultaneous async (parallel) operations ....................................................................................... 238 Chapter 41: Strings .................................................................................................................................................. 239 Section 41.1: Basic Info and String Concatenation ................................................................................................. 239 Section 41.2: Reverse String ..................................................................................................................................... 240 Section 41.3: Comparing Strings Lexicographically ............................................................................................... 241 Section 41.4: Access character at index in string ................................................................................................... 242 Section 41.5: Escaping quotes .................................................................................................................................. 242 Section 41.6: Word Counter ...................................................................................................................................... 242 Section 41.7: Trim whitespace .................................................................................................................................. 243 Section 41.8: Splitting a string into an array ........................................................................................................... 243 Section 41.9: Strings are unicode ............................................................................................................................. 243 Section 41.10: Detecting a string .............................................................................................................................. 244 Section 41.11: Substrings with slice ........................................................................................................................... 244 Section 41.12: Character code .................................................................................................................................. 245 Section 41.13: String Representations of Numbers ................................................................................................ 245 Section 41.14: String Find and Replace Functions .................................................................................................. 246 Section 41.15: Find the index of a substring inside a string ................................................................................... 247 Section 41.16: String to Upper Case ......................................................................................................................... 247 Section 41.17: String to Lower Case ......................................................................................................................... 247 Section 41.18: Repeat a String .................................................................................................................................. 247 Chapter 42: Constructor functions ................................................................................................................. 247 Section 42.1: Declaring a constructor function ....................................................................................................... 247 Chapter 43: execCommand and contenteditable ................................................................................... 248 Section 43.1: Listening to Changes of contenteditable .......................................................................................... 249 Section 43.2: Getting started .................................................................................................................................... 249 Section 43.3: Copy to clipboard from textarea using execCommand("copy") .................................................. 250 Section 43.4: Formatting .......................................................................................................................................... 251 Chapter 44: Performance Tips ......................................................................................................................... 251 Section 44.1: Avoid try/catch in performance-critical functions .......................................................................... 251 Section 44.2: Limit DOM Updates ............................................................................................................................ 252 Section 44.3: Benchmarking your code - measuring execution time .................................................................. 252 Section 44.4: Use a memoizer for heavy-computing functions ........................................................................... 254 Section 44.5: Initializing object properties with null ............................................................................................... 256 Section 44.6: Reuse objects rather than recreate ................................................................................................. 257 Section 44.7: Prefer local variables to globals, attributes, and indexed values .................................................. 258 Section 44.8: Be consistent in use of Numbers ...................................................................................................... 259 Chapter 45: Map ....................................................................................................................................................... 260 Section 45.1: Creating a Map .................................................................................................................................... 260 Section 45.2: Clearing a Map ................................................................................................................................... 260 Section 45.3: Removing an element from a Map .................................................................................................. 260 Section 45.4: Checking if a key exists in a Map ...................................................................................................... 261 Section 45.5: Iterating Maps ..................................................................................................................................... 261 Section 45.6: Getting and setting elements ............................................................................................................ 261 Section 45.7: Getting the number of elements of a Map ...................................................................................... 262 Chapter 46: Creational Design Patterns ...................................................................................................... 262 Section 46.1: Factory Functions ............................................................................................................................... 262 Section 46.2: Factory with Composition ................................................................................................................. 263 Section 46.3: Module and Revealing Module Patterns .......................................................................................... 264 Section 46.4: Prototype Pattern ............................................................................................................................... 266 Section 46.5: Singleton Pattern ................................................................................................................................ 267 Section 46.6: Abstract Factory Pattern ................................................................................................................... 268 Chapter 47: requestAnimationFrame ............................................................................................................ 268 Section 47.1: Use requestAnimationFrame to fade in element ............................................................................ 268 Section 47.2: Keeping Compatability ...................................................................................................................... 269 Section 47.3: Cancelling an Animation .................................................................................................................... 269 Chapter 48: Reserved Keywords ..................................................................................................................... 270 Section 48.1: Reserved Keywords ............................................................................................................................ 270 Section 48.2: Identifiers & Identifier Names ........................................................................................................... 272 Chapter 49: Method Chaining ............................................................................................................................ 273 Section 49.1: Chainable object design and chaining .............................................................................................. 273 Section 49.2: Method Chaining ................................................................................................................................ 276 Chapter 50: Global error handling in browsers ........................................................................................ 276 Section 50.1: Handling window.onerror to report all errors back to the server-side ......................................... 277 Chapter 51: Unary Operators ............................................................................................................................. 277 Section 51.1: Overview ............................................................................................................................................... 278 Section 51.2: The typeof operator ........................................................................................................................... 278 Section 51.3: The delete operator ............................................................................................................................ 279

Description:
Section 3.1: Converting Array-like Objects to Arrays Section 44.4: Use a memoizer for heavy-computing functions Before ES6, named functions had their name properties set to their function names, and function inc() { A catch allows a rejected promise to recover, similar to how catch in a
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.