|
3 | 3 | ```
|
4 | 4 | This review covers:
|
5 | 5 | • More CLI
|
6 |
| -• Closures |
7 |
| -• Scope |
| 6 | +• Scope, closures and 'this' |
8 | 7 | • Array Manipulations
|
9 |
| -• Basic DOM manipulations [img src, innerHTML] |
| 8 | +• Basic DOM manipulations |
10 | 9 | • Code commenting
|
11 | 10 | ```
|
12 | 11 |
|
13 |
| -Check out the CLI review here: https://github.com/HackYourFuture/CommandLine/blob/master/Lecture-2.md |
| 12 | +## More CLI |
| 13 | +Check out the CLI review here: https://github.com/HackYourFuture/CommandLine/blob/master/Lecture-2.md |
| 14 | + |
| 15 | +## Scope, closures and 'this' |
| 16 | +Scope, closure and 'this' are about *context*. |
| 17 | + |
| 18 | +This post explains things really well: [Recommended read by Todd Motto on Scope](https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/) |
| 19 | + |
| 20 | +In this review we won't go over how JavaScript implements scope. We would just be rewriting the above post by Todd Motto. |
| 21 | + |
| 22 | +Instead, let's focus on a couple of important **words** that are used in explaining scope. Understanding the JavaScript side of things can be difficult if we don't fully understand these words. |
| 23 | + |
| 24 | +### How to think about context? |
| 25 | +Consider the following sentences: |
| 26 | +> Eyad is a great cook. *He* loves to make ravioli. |
| 27 | +
|
| 28 | +> Gijs is a great cook. *He* loves to make ravioli. |
| 29 | +
|
| 30 | +In both cases, the second sentence is the exact same. However, the word *he* refers to a completely different person. He is defined by the context. |
| 31 | + |
| 32 | +A second example: |
| 33 | +> *He* loves to make ravioli. |
| 34 | +
|
| 35 | +Now, when this sentence is written down without *he* being defined in the context, the sentence doesn't make any sense. |
| 36 | + |
| 37 | +### Context in programming: |
| 38 | +A JavaScript example: |
| 39 | +``` |
| 40 | +function myFunction() { |
| 41 | + const a = 'ravioli' |
| 42 | + console.log(a) |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +``` |
| 47 | +function myFunction() { |
| 48 | + console.log(a) |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +In both cases, `console.log(a)` is the exact same. However, the context defines the value of a and whether it is defined at all. |
| 53 | + |
| 54 | +### The Scope of the Context |
| 55 | +Let's first look at a definition of `scope` |
| 56 | +> (1) the extent of the area or subject matter that something deals with or to which it is relevant. |
| 57 | +> (2) the opportunity or possibility to do or deal with something. |
| 58 | +
|
| 59 | +So in words more applicable to our situation scope means: |
| 60 | +> code that is within the reach of our code. |
| 61 | +
|
| 62 | +Consider two completely different JavaScript files |
| 63 | +``` |
| 64 | +// aFile.js |
| 65 | +const a = 10 |
| 66 | +``` |
| 67 | + |
| 68 | +``` |
| 69 | +// anotherFile.js |
| 70 | +console.log(a) |
| 71 | +``` |
| 72 | + |
| 73 | +When we run these files separately, the `console.log(a)` statement in anotherFile.js of course cannot reach `var a = 10`. Why? It is beyond the scope of our context. When we run a file in JavaScript, the program creates a new top-level context we call the global scope. |
| 74 | + |
| 75 | +From now on, we'll call 'scoped context' simply 'scope'. |
| 76 | + |
| 77 | +### Creating Scope within a Program |
| 78 | +Just like two programs have an completely different scope, we can also create different scopes within a program. We do the same in our language: |
| 79 | +> Eyad is a great cook. *He* loves to make ravioli. Gijs is great at finding the best ingredients. *He* has a real nose for it. |
| 80 | +
|
| 81 | +At school one learns that *he* will refer to the last masculine subject introduced to the text. There are rules constraining this. In programming we rely a lot on context, and the rules are more strict than in natural language. |
| 82 | + |
| 83 | +There are *five different ways* we can create a new context in JavaScript: |
| 84 | +- The global context (as we already saw) |
| 85 | +- The simple function context |
| 86 | +- The object construction context |
| 87 | +- The object method context |
| 88 | +- The event listener context |
| 89 | + |
| 90 | +More info on this in this great post |
| 91 | + |
| 92 | +## Array Manipulation |
| 93 | +[MDN on Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) |
| 94 | +As we know by now, arrays are collections of values. |
| 95 | + |
| 96 | +As we will see, there are often many ways to achieve the same thing when working arrays. Over time, you will add different techniques to your mental toolbox to achieve the result you want quickly. |
| 97 | + |
| 98 | +The basics. |
| 99 | + |
| 100 | +- How do we create an array? |
| 101 | +- How do we add items to an array? |
| 102 | +- How do we change items of an array? |
| 103 | +- How do we remove items from an array? |
| 104 | +- How do we know the length of an array? |
| 105 | +- How do we iterate over an array? |
| 106 | + |
| 107 | + |
| 108 | +### How do we create an array? |
| 109 | +An array can be created multiple ways |
| 110 | + |
| 111 | +From scratch: |
| 112 | +``` |
| 113 | +const a = [] // result: [] |
| 114 | +const b = ['item1', 'item2'] // result: ['item1', 'item2'] |
| 115 | +const c = new Array() // result: [] |
| 116 | +const d = new Array('item 1', 'item2') // result: ['item1', 'item2'] |
| 117 | +const e = new Array(20) // result: [ <20 empty items> ] |
| 118 | +const f = new Array(20, 21) // result: [20, 21] |
| 119 | +// Note that `e` and `f` are a beautiful example of how weird and unexpected JavaScript can be. You will probably use `a` most often. |
| 120 | +``` |
| 121 | + |
| 122 | +From value (as an example, many ways to create an array from another value): |
| 123 | +``` |
| 124 | +const a = 'hello world' // result: 'hello world' |
| 125 | +const b = a.split(' ') // result: ['hello', 'world' ] |
| 126 | +``` |
| 127 | + |
| 128 | +### Array length |
| 129 | +Every array has as a 'static' property `length`. Meaning that we can easily get the amount of items in an array. |
| 130 | +``` |
| 131 | +const f = ['hi','there'] |
| 132 | +console.log(f.length) // 2 |
| 133 | +``` |
| 134 | + |
| 135 | +### Array index |
| 136 | +We can access array elements through the position of the element in the array. This is called an index. Indices (plural of index) are 0-based, meaning that the first item's index is 0, the second element is 1. |
| 137 | + |
| 138 | +``` |
| 139 | +const x = ['first', 'second', 'third'] |
| 140 | +console.log(x[0]) // 'first' |
| 141 | +
|
| 142 | +x[3] = 'fourth' |
| 143 | +``` |
| 144 | + |
| 145 | +Note that arrays can have empty values. This should be avoided usually to prevent unexpected behaviour. |
| 146 | +``` |
| 147 | +x[10] = 'eleventh' |
| 148 | +console.log(x) // [ 'first', 'second', 'third', 'fourth', <6 empty items>, 'eleventh' ] |
| 149 | +``` |
| 150 | + |
| 151 | +Next to the index, we have a wide range of tools to manipulate arrays. |
| 152 | + |
| 153 | +### Array methods |
| 154 | +These methods are essential. |
| 155 | + |
| 156 | +**Extremely important is to remember to always ask these two questions**: |
| 157 | +• What is the return value of this method? |
| 158 | +• What does this method do to the original array it is used on? |
| 159 | + |
| 160 | +**Adding items** |
| 161 | +• [`.push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Add item to end of array |
| 162 | +• [`.unshift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) Add item to beginning of array |
| 163 | + |
| 164 | +**Removing items** |
| 165 | +• [`.shift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Remove first element from array |
| 166 | +• [`.pop()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) Remove last element from array |
| 167 | +• [`.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) Remove a specific element from array using index |
| 168 | + |
| 169 | +**Useful iterators over arrays** |
| 170 | +• [`.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) creates a new array with the results of calling a provided function on every element in the calling array. |
| 171 | +• [`.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) creates a new array with all elements that pass the test implemented by the provided function. |
| 172 | +• [`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) sorts the elements of an array in place and returns the array |
| 173 | +``` |
| 174 | +
|
| 175 | +## Basic DOM manipulations |
| 176 | +Using JavaScript we can access and manipulate the Document Object Model (DOM). We access the DOM through a global object called `document`. |
| 177 | +
|
| 178 | +HTML |
| 179 | +``` |
| 180 | +<body> |
| 181 | + <div id="hello"></div> |
| 182 | +</body> |
| 183 | +``` |
| 184 | +
|
| 185 | +A common method to access the DOM is by giving a HTML element an ID, and then using the `document` method `getElementById()` |
| 186 | +
|
| 187 | +``` |
| 188 | +const x = document.getElementById('hello') |
| 189 | +``` |
| 190 | +
|
| 191 | +Now we have stored a *reference* of how that HTML element is accessed through the DOM object. We can use this to manipulate the element. |
| 192 | +
|
| 193 | +``` |
| 194 | +x.innerHTML = 'hello' |
| 195 | +``` |
| 196 | +
|
| 197 | +We can also create elements |
| 198 | +``` |
| 199 | +const a = document.createElement('li') |
| 200 | +x.appendChild(a) |
| 201 | +``` |
| 202 | +
|
| 203 | +
|
| 204 | +
|
| 205 | +## Code Commenting |
| 206 | +First the straightforward part: how do we place comments in our code? |
| 207 | +
|
| 208 | +### JavaScript |
| 209 | +Single line comments |
| 210 | +``` |
| 211 | +// Change heading: |
| 212 | +document.getElementById("myH").innerHTML = "My First Page"; |
| 213 | +``` |
| 214 | +
|
| 215 | +Single line comments at end of the line: |
| 216 | +``` |
| 217 | +var x = 5; // Declare x, give it the value of 5 |
| 218 | +``` |
| 219 | +
|
| 220 | +Coding **well** in JavaScript: [JSDoc](http://usejsdoc.org/) |
| 221 | +
|
| 222 | +### HTML |
| 223 | +[W3Schools](https://www.w3schools.com/html/html_comments.asp) |
| 224 | +Comments |
| 225 | +``` |
| 226 | +<!-- Write |
| 227 | +your comments here --> |
| 228 | + |
| 229 | +<!-- Write your comments here --> |
| 230 | +``` |
| 231 | +
|
| 232 | +
|
| 233 | +### CSS |
| 234 | +[MDN on CSS comments](https://developer.mozilla.org/en-US/docs/Web/CSS/Comments) |
| 235 | +``` |
| 236 | +/* Comment */ |
| 237 | + |
| 238 | +/* |
| 239 | +A comment |
| 240 | +which stretches |
| 241 | +over several |
| 242 | +lines |
| 243 | +*/ |
| 244 | +``` |
| 245 | +
|
| 246 | +### When to comment? |
| 247 | +Now for the hard part: when to comment? When you work for different companies, you will see different styles. Embrace something you like, and then learn to let go. Google on "when to comment code?" and you'll find a big bunch of different opinions. |
| 248 | +
|
| 249 | +The general concept is, however, that it is there to help make the code more easy to understand. Note, however, that comments can also make code more difficult to understand when not applied properly. |
| 250 | +
|
| 251 | +
|
| 252 | +
|
| 253 | +
|
| 254 | +
|
| 255 | +
|
| 256 | +
|
| 257 | +
|
| 258 | +
|
0 commit comments