You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Week3/REVIEW.md
+64-73Lines changed: 64 additions & 73 deletions
Original file line number
Diff line number
Diff line change
@@ -2,75 +2,75 @@
2
2
3
3
```
4
4
This review covers:
5
-
• More CLI
6
-
• Scope, closures and 'this'
7
-
• Array Manipulations
8
-
• Basic DOM manipulations
9
-
• Code commenting
5
+
- More CLI
6
+
- Scope, closures and 'this'
7
+
- Array Manipulations
8
+
- Basic DOM manipulations
9
+
- Code commenting
10
10
```
11
11
12
12
## More CLI
13
13
Check out the CLI review here: https://github.com/HackYourFuture/CommandLine/blob/master/Lecture-2.md
14
14
15
15
## Scope, closures and 'this'
16
-
Scope, closure and 'this' are about *context*.
16
+
Scope, closure and 'this' are about *context*.
17
17
18
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
19
20
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
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.
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
23
24
24
### How to think about context?
25
-
Consider the following sentences:
25
+
Consider the following sentences:
26
26
> Eyad is a great cook. *He* loves to make ravioli.
27
27
28
-
> Gijs is a great cook. *He* loves to make ravioli.
28
+
> Gijs is a great cook. *He* loves to make ravioli.
29
29
30
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
31
32
32
A second example:
33
33
> *He* loves to make ravioli.
34
34
35
-
Now, when this sentence is written down without *he* being defined in the context, the sentence doesn't make any sense.
35
+
Now, when this sentence is written down without *he* being defined in the context, the sentence doesn't make any sense.
36
36
37
37
### Context in programming:
38
-
A JavaScript example:
38
+
A JavaScript example:
39
39
```js
40
40
functionmyFunction() {
41
-
consta='ravioli'
42
-
console.log(a)
41
+
consta='ravioli';
42
+
console.log(a);
43
43
}
44
44
```
45
45
46
46
```js
47
47
functionmyFunction() {
48
-
console.log(a)
48
+
console.log(a);
49
49
}
50
50
```
51
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.
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
53
54
-
### The Scope of the Context
54
+
### The Scope of the Context
55
55
Let's first look at a definition of `scope`
56
56
> (1) the extent of the area or subject matter that something deals with or to which it is relevant.
57
57
> (2) the opportunity or possibility to do or deal with something.
58
58
59
59
So in words more applicable to our situation scope means:
60
-
> code that is within the reach of our code.
60
+
> code that is within the reach of our code.
61
61
62
62
Consider two completely different JavaScript files
63
63
```js
64
64
// aFile.js
65
-
consta=10
65
+
consta=10;
66
66
```
67
67
68
68
```js
69
69
// anotherFile.js
70
-
console.log(a)
70
+
console.log(a);
71
71
```
72
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.
73
+
When we run these files separately, the `console.log(a)` statement in anotherFile.js of course cannot reach `const 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
74
75
75
From now on, we'll call 'scoped context' simply 'scope'.
76
76
@@ -82,20 +82,20 @@ At school one learns that *he* will refer to the last masculine subject introduc
82
82
83
83
There are *five different ways* we can create a new context in JavaScript:
84
84
- The global context (as we already saw)
85
-
- The simple function context
86
-
- The object construction context
85
+
- The simple function context
86
+
- The object construction context
87
87
- The object method context
88
88
- The event listener context
89
89
90
90
More info on this [in this great post](https://zellwk.com/blog/this/)
91
91
92
92
## Array Manipulation
93
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.
94
+
As we know by now, arrays are collections of values.
95
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.
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.
// 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
120
```
121
121
122
122
From value (as an example, many ways to create an array from another value):
123
123
```js
124
-
consta='hello world'// result: 'hello world'
125
-
constb=a.split('') // result: ['hello', 'world' ]
124
+
consta='hello world';// result: 'hello world'
125
+
constb=a.split('');// result: ['hello', 'world' ]
126
126
```
127
127
128
128
### Array length
129
129
Every array has as a 'static' property `length`. Meaning that we can easily get the amount of items in an array.
130
130
```js
131
-
constf= ['hi','there']
132
-
console.log(f.length) // 2
131
+
constf= ['hi','there'];
132
+
console.log(f.length);// 2
133
133
```
134
134
135
135
### Array index
136
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
137
138
138
```js
139
-
constx= ['first', 'second', 'third']
140
-
console.log(x[0]) // 'first'
139
+
constx= ['first', 'second', 'third'];
140
+
console.log(x[0]);// 'first'
141
141
142
-
x[3] ='fourth'
142
+
x[3] ='fourth';
143
143
```
144
144
145
-
Note that arrays can have empty values. This should be avoided usually to prevent unexpected behaviour.
145
+
Note that arrays can have empty values. This should be avoided usually to prevent unexpected behaviour.
Next to the index, we have a wide range of tools to manipulate arrays.
152
152
153
153
### Array methods
154
-
These methods are essential.
154
+
These methods are essential.
155
155
156
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?
157
+
- What is the return value of this method?
158
+
- What does this method do to the original array it is used on?
159
159
160
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
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
163
164
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
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
168
169
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
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
173
174
174
175
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`.
176
+
Using JavaScript we can access and manipulate the Document Object Model (DOM). We access the DOM through a global object called `document`.
177
177
178
-
HTML
178
+
HTML
179
179
```html
180
180
<body>
181
-
<divid="hello"></div>
181
+
<divid="hello"></div>
182
182
</body>
183
183
```
184
184
185
185
A common method to access the DOM is by giving a HTML element an ID, and then using the `document` method `getElementById()`
186
186
187
187
```js
188
-
constx=document.getElementById('hello')
188
+
constx=document.getElementById('hello');
189
189
```
190
190
191
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
192
193
193
```js
194
-
x.innerHTML='hello'
194
+
x.innerHTML='hello';
195
195
```
196
196
197
197
We can also create elements
198
198
```js
199
-
consta=document.createElement('li')
200
-
x.appendChild(a)
199
+
consta=document.createElement('li');
200
+
x.appendChild(a);
201
201
```
202
202
203
203
@@ -223,7 +223,7 @@ Coding **well** in JavaScript: [JSDoc](http://usejsdoc.org/)
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
-
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.
258
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.
0 commit comments