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
+7-211Lines changed: 7 additions & 211 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,194 +13,24 @@ This review covers:
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*.
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
-
```js
40
-
functionmyFunction() {
41
-
consta='ravioli';
42
-
console.log(a);
43
-
}
44
-
```
45
-
46
-
```js
47
-
functionmyFunction() {
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
-
```js
64
-
// aFile.js
65
-
consta=10;
66
-
```
67
16
68
-
```js
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 `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
-
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
17
+
Scope, closure and 'this' are about *context*.
89
18
90
-
More info on this [in this great post](https://zellwk.com/blog/this/)
[MDN on Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
22
+
94
23
As we know by now, arrays are collections of values.
95
24
96
25
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.
Every array has as a 'static' property `length`. Meaning that we can easily get the amount of items in an array.
130
-
```js
131
-
constf= ['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
-
```js
139
-
constx= ['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.
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
[MDN on CSS comments](https://developer.mozilla.org/en-US/docs/Web/CSS/Comments)
235
-
```css
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.
[MDN on Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
4
+
As we know by now, arrays are collections of values.
5
+
6
+
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.
Every array has as a 'static' property `length`. Meaning that we can easily get the amount of items in an array.
40
+
```js
41
+
constf= ['hi','there'];
42
+
console.log(f.length); // 2
43
+
```
44
+
45
+
## Array index
46
+
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.
47
+
48
+
```js
49
+
constx= ['first', 'second', 'third'];
50
+
console.log(x[0]); // 'first'
51
+
52
+
x[3] ='fourth';
53
+
```
54
+
55
+
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.
62
+
63
+
## Array methods
64
+
These methods are essential.
65
+
66
+
**Extremely important is to remember to always ask these two questions**:
67
+
- What is the return value of this method?
68
+
- What does this method do to the original array it is used on?
69
+
70
+
**Adding items**
71
+
-[`.push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Add item to end of array
72
+
-[`.unshift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) Add item to beginning of array
73
+
74
+
**Removing items**
75
+
-[`.shift()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) Remove first element from array
76
+
-[`.pop()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) Remove last element from array
77
+
-[`.splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) Remove a specific element from array using index
78
+
79
+
**Useful iterators over arrays**
80
+
-[`.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.
81
+
-[`.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.
82
+
-[`.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
[MDN on CSS comments](https://developer.mozilla.org/en-US/docs/Web/CSS/Comments)
31
+
```css
32
+
/* Comment */
33
+
34
+
/*
35
+
A comment
36
+
which stretches
37
+
over several
38
+
lines
39
+
*/
40
+
```
41
+
42
+
## When to comment?
43
+
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.
44
+
45
+
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