|
| 1 | +// Compare Scopes of the var and let Keywords |
| 2 | +function checkScope() { |
| 3 | +"use strict"; |
| 4 | + let i = "function scope"; |
| 5 | + if (true) { |
| 6 | + let i = "block scope"; |
| 7 | + console.log("Block scope i is: ", i); |
| 8 | + } |
| 9 | + console.log("Function scope i is: ", i); |
| 10 | + return i; |
| 11 | +} |
| 12 | + |
| 13 | +// Mutate an Array Declared with const |
| 14 | +const s = [5, 7, 2]; |
| 15 | +function editInPlace() { |
| 16 | + "use strict"; |
| 17 | + s[0] = 2; |
| 18 | + s[1] = 5; |
| 19 | + s[2] = 7; |
| 20 | +} |
| 21 | +editInPlace(); |
| 22 | + |
| 23 | +// Prevent Object Mutation |
| 24 | +function freezeObj() { |
| 25 | + "use strict"; |
| 26 | + const MATH_CONSTANTS = { |
| 27 | + PI: 3.14 |
| 28 | + }; |
| 29 | + Object.freeze(MATH_CONSTANTS); |
| 30 | + try { |
| 31 | + MATH_CONSTANTS.PI = 99; |
| 32 | + } catch(ex) { |
| 33 | + console.log(ex); |
| 34 | + } |
| 35 | + return MATH_CONSTANTS.PI; |
| 36 | +} |
| 37 | + |
| 38 | +// Use Arrow Functions to Write Concise Anonymous Functions |
| 39 | +const magic = () => new Date(); |
| 40 | + |
| 41 | +// Write Arrow Functions with Parameters |
| 42 | +const myConcat = (arr1, arr2) => arr1.concat(arr2); |
| 43 | + |
| 44 | +// Set Default Parameters for Your Functions |
| 45 | +const increment = (number, value = 1) => number + value; |
| 46 | + |
| 47 | +// Use the Rest Parameter with Function Parameters |
| 48 | +const sum = (...args) => args.reduce((a, b) => a + b, 0); |
| 49 | + |
| 50 | +// Use the Spread Operator to Evaluate Arrays In-Place |
| 51 | +const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY']; |
| 52 | +let arr2; |
| 53 | +(function() { |
| 54 | + "use strict"; |
| 55 | + arr2 = [...arr1]; |
| 56 | +})(); |
| 57 | +console.log(arr2); |
| 58 | + |
| 59 | +// Use Destructuring Assignment to Extract Values from Objects |
| 60 | +const HIGH_TEMPERATURES = { |
| 61 | + yesterday: 75, |
| 62 | + today: 77, |
| 63 | + tomorrow: 80 |
| 64 | +}; |
| 65 | +const {today, tomorrow} = HIGH_TEMPERATURES; |
| 66 | + |
| 67 | +// Use Destructuring Assignment to Assign Variables from Objects |
| 68 | +const HIGH_TEMPERATURES0 = { |
| 69 | + yesterday: 75, |
| 70 | + today: 77, |
| 71 | + tomorrow: 80 |
| 72 | +}; |
| 73 | + |
| 74 | +// Use Destructuring Assignment to Assign Variables from Objects |
| 75 | +const {today: highToday4, tomorrow: highTomorrow} = HIGH_TEMPERATURES; |
| 76 | + |
| 77 | +// Use Destructuring Assignment to Assign Variables from Nested Objects |
| 78 | +const LOCAL_FORECAST = { |
| 79 | + yesterday: { low: 61, high: 75 }, |
| 80 | + today: { low: 64, high: 77 }, |
| 81 | + tomorrow: { low: 68, high: 80 } |
| 82 | + }; |
| 83 | + |
| 84 | +const {today: {low: lowToday, high: highToday}} = LOCAL_FORECAST; |
| 85 | + |
| 86 | +// Use Destructuring Assignment to Assign Variables from Arrays |
| 87 | +let a = 8, b = 6; |
| 88 | +(() => { |
| 89 | + "use strict"; |
| 90 | + [a, b] = [b, a]; |
| 91 | +})(); |
| 92 | +console.log(a); |
| 93 | + |
| 94 | +// Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements |
| 95 | +const source = [1,2,3,4,5,6,7,8,9,10]; |
| 96 | +function removeFirstTwo(list) { |
| 97 | +"use strict"; |
| 98 | + const [a, b, ...arr] = list; |
| 99 | + return arr; |
| 100 | +} |
| 101 | +const arr = removeFirstTwo(source); |
| 102 | + |
| 103 | +// Use Destructuring Assignment to Pass an Object as a Function's Parameters |
| 104 | +const stats = { |
| 105 | + max: 56.78, |
| 106 | + standard_deviation: 4.34, |
| 107 | + median: 34.54, |
| 108 | + mode: 23.87, |
| 109 | + min: -0.75, |
| 110 | + average: 35.85 |
| 111 | +}; |
| 112 | +const half = ({max, min}) => (max + min) / 2.0; |
| 113 | +console.log(half(stats)); |
| 114 | + |
| 115 | +// Create Strings using Template Literals |
| 116 | +const result = { |
| 117 | + success: ["max-length", "no-amd", "prefer-arrow-functions"], |
| 118 | + failure: ["no-var", "var-on-top", "linebreak"], |
| 119 | + skipped: ["no-extra-semi", "no-dup-keys"] |
| 120 | +}; |
| 121 | +function makeList(arr){ |
| 122 | +"use strict"; |
| 123 | + const resultDisplayArray = arr.map(item => `<li class="text-warning">${item}</li>`); |
| 124 | + return resultDisplayArray; |
| 125 | +} |
| 126 | +const resultDisplayArray = makeList(result.failure); |
| 127 | + |
| 128 | +// Write Concise Object Literal Declarations Using Object Property Shorthand |
| 129 | +const createPerson = (name, age, gender) =>{ |
| 130 | + return { |
| 131 | + |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// Write Concise Declarative Functions with ES6 |
| 136 | +const bicycle = { |
| 137 | + gear: 2, |
| 138 | + setGear(newGear) { |
| 139 | + this.gear = newGear; |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +// Use class Syntax to Define a Constructor Function |
| 144 | +class Vegetable { |
| 145 | + constructor(name){ |
| 146 | + this.name = name; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +const carrot = new Vegetable('carrot'); |
| 151 | +console.log(carrot.name); |
| 152 | + |
| 153 | +// Use getters and setters to Control Access to an Object |
| 154 | +class Thermostat { |
| 155 | + constructor(fahrenheit){ |
| 156 | + this.fahrenheit = fahrenheit; |
| 157 | + } |
| 158 | + get temperature(){ |
| 159 | + return 5/9 * (this.fahrenheit - 32); |
| 160 | + } |
| 161 | + set temperature(celsius){ |
| 162 | + this.fahrenheit = celsius * 9.0 / 5 + 32; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +const thermos = new Thermostat(76); |
| 167 | +let temp = thermos.temperature; |
| 168 | +thermos.temperature = 26; |
| 169 | +temp = thermos.temperature; |
| 170 | + |
| 171 | +// Create a Module Script |
| 172 | +<html> |
| 173 | + <body> |
| 174 | +<script type="module" src="index.js"></script> |
| 175 | + |
| 176 | + </body> |
| 177 | +</html> |
| 178 | + |
| 179 | +// Use export to Share a Code Block |
| 180 | +const uppercaseString = (string) => { |
| 181 | + return string.toUpperCase(); |
| 182 | +} |
| 183 | + |
| 184 | +const lowercaseString = (string) => { |
| 185 | + return string.toLowerCase() |
| 186 | +} |
| 187 | + |
| 188 | +export { uppercaseString, lowercaseString }; |
| 189 | + |
| 190 | +// Reuse JavaScript Code Using import |
| 191 | +import { uppercaseString, lowercaseString } from './string_functions.js'; |
| 192 | +uppercaseString("hello"); |
| 193 | +lowercaseString("WORLD!"); |
| 194 | + |
| 195 | +// Use * to Import Everything from a File |
| 196 | +import * as stringFunctions from "./string_functions.js"; |
| 197 | +stringFunctions.uppercaseString("hello"); |
| 198 | +stringFunctions.lowercaseString("WORLD!"); |
| 199 | + |
| 200 | +// Create an Export Fallback with export default |
| 201 | +export default function subtract(x, y) { |
| 202 | + return x - y; |
| 203 | +} |
| 204 | + |
| 205 | +// Import a Default Export |
| 206 | +import subtract from "./math_functions.js"; |
| 207 | +subtract(7,4); |
| 208 | + |
| 209 | +// Create a JavaScript Promise |
| 210 | +const makeServerRequest = new Promise((resolve, reject) => {}); |
| 211 | + |
| 212 | +// Complete a Promise with resolve and reject |
| 213 | +const makeServerRequest8 = new Promise((resolve, reject) => { |
| 214 | + let responseFromServer = true; |
| 215 | + if(responseFromServer){ |
| 216 | + resolve("We got the data"); |
| 217 | + } else { |
| 218 | + reject("Data not received"); |
| 219 | + } |
| 220 | +}); |
| 221 | + |
| 222 | +// Handle a Fulfilled Promise with then |
| 223 | +const makeServerRequest4 = new Promise((resolve, reject) => { |
| 224 | + let responseFromServer = true; |
| 225 | + if(responseFromServer){ |
| 226 | + resolve("We got the data"); |
| 227 | + } else { |
| 228 | + reject("Data not received"); |
| 229 | + } |
| 230 | +}); |
| 231 | +makeServerRequest.then(result => { |
| 232 | + console.log(result); |
| 233 | +}); |
| 234 | + |
| 235 | +// Handle a Rejected Promise with catch |
| 236 | +const makeServerRequest2 = new Promise((resolve, reject) => { |
| 237 | + let responseFromServer = false; |
| 238 | + if(responseFromServer){ |
| 239 | + resolve("We got the data"); |
| 240 | + } else { |
| 241 | + reject("Data not received"); |
| 242 | + } |
| 243 | +}); |
| 244 | +makeServerRequest.then(result => { |
| 245 | + console.log(result); |
| 246 | +}); |
| 247 | +makeServerRequest.catch(error => { |
| 248 | + console.log(error); |
| 249 | +}); |
| 250 | + |
0 commit comments