Add lesson 4: Objects and JSON
3 changed files with 35 additions and 0 deletions
lessons/04-objects-and-json/README.md ADDED
| @@ -0,0 +1,19 @@ | ||
| 1 | +# Lesson 4: Objects and JSON | |
| 2 | + | |
| 3 | +We look at objects and JSON the way working developers use it, with examples you can change and run right away. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/javascript-web/lessons/4](https://glibcode.net/courses/javascript-web/lessons/4) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `objects.js`: objects, destructuring and JSON | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +node objects.js | |
| 15 | +``` | |
| 16 | + | |
| 17 | +## Exercises | |
| 18 | + | |
| 19 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/04-objects-and-json/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 4 | |
| 2 | + | |
| 3 | +1. Time yourself: rewrite the example from memory in 10 minutes, then diff it against the original. | |
| 4 | +2. Combine this lesson with the previous one: use something from both in a small program of your own. | |
| 5 | +3. What happens with an empty input? Change the code so it handles that case with a clear message. | |
lessons/04-objects-and-json/objects.js ADDED
| @@ -0,0 +1,11 @@ | ||
| 1 | +const book = { title: "Dune", author: "Frank Herbert", year: 1965, tags: ["sci-fi"] }; | |
| 2 | + | |
| 3 | +const { title, year } = book; | |
| 4 | +console.log(`${title} (${year})`); | |
| 5 | + | |
| 6 | +const text = JSON.stringify(book); | |
| 7 | +console.log(text); | |
| 8 | + | |
| 9 | +const copy = JSON.parse(text); | |
| 10 | +copy.tags.push("classic"); | |
| 11 | +console.log(book.tags, copy.tags); | |