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: Week2/README.md
+46-4Lines changed: 46 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,19 @@ function aFunction() {
24
24
}
25
25
```
26
26
27
+
You did so using an old version of JavaScript.
28
+
29
+
But if you've ever used `arrow` functions (which you'll learn more about in the next section), you did so using a newer version of JavaScript.
30
+
31
+
That's good and all, but why is this important to differentiate? There are several reasons:
32
+
33
+
- Each feature (and its updates) of a language is made to solve a specific problem. It's important to know the context and purpose of each in order to know how to use it
34
+
- Software is always evolving. This means that there are different versions that different users might be using. This means not every feature will work for every application.
35
+
36
+
That's why it's important to know a little about the history of JavaScript: it will make you think of JavaScript (and hopefully software in general) as a continually evolving thing, as opposed to "just a bunch of concepts and techniques you need to memorize".
37
+
38
+
Check the following resources out to learn more about this:
39
+
27
40
-[The History of JavaScript | Why is JavaScript also called ECMAScript?](https://www.youtube.com/watch?v=JpwxjkpZfhY)
28
41
-[The Weird History of JavaScript](https://www.youtube.com/watch?v=Sh6lK57Cuk4)
29
42
@@ -37,7 +50,7 @@ But what if you want to have callbacks within callbacks... within callbacks? Thi
37
50
38
51
-[Callback Hell](http://callbackhell.com/)
39
52
40
-
This is where Promises come in. The concept of a Promise, in execution, doesn't add anything new. It does exactly what callbacks aim to do, which is enabling asynchronous actions: for example, clicking a button to load in an image, while still being able to navigate the webpage.
53
+
This is where `Promises` come in. The concept of a Promise, in execution, doesn't add anything new. It does exactly what callbacks aim to do, which is enabling asynchronous actions: for example, clicking a button to load in an image, while still being able to navigate the webpage.
41
54
42
55
What a Promise does is make writing callbacks more readable for you, the developer. This is its main benefit. In effect, you could call Promises the updated version of callbacks. Callbacks version 2.0.
43
56
@@ -49,18 +62,47 @@ Go through the following resources to learn more about Promises:
49
62
50
63
## 3. Arrow functions
51
64
52
-
One of a programmer's favorite things to do is to write clean and concise code. Arrow functions are a new way within
65
+
One of a programmer's favorite things to do is to write clean and concise code. Arrow functions are a new way to write functions, given to us by the ECMAScript 6 (The software standard JavaScript is based upon) update of JavaScript.
66
+
67
+
It is written like this:
68
+
69
+
```js
70
+
// Arrow function
71
+
() => {};
72
+
```
53
73
74
+
Go through the following resources to learn more about why they're important:
-[When (and why) you should use ES6 arrow functions — and when you shouldn’t](https://www.freecodecamp.org/news/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26/)
79
+
80
+
### The `this` keyword
81
+
82
+
In JavaScript, like in any other programming language you'll find, there are certain special keywords that always create a specific effect. The `this` keyword is one of those.
83
+
84
+
In everyday communication we use words like "this" or "that" whenever we want to refer to things in the world or something someone said. It's similarly used in JavaScript.
85
+
86
+
Simply put: `this` refers to any object it's defined in. The global object, `window` is the default value of `this`. However, anything a new object is created will have its own `this` value.
87
+
88
+
Go through the following resources to learn more about `this`:
89
+
90
+
-[Understanding "this" in JavaScript](https://www.codementor.io/dariogarciamoya/understanding--this--in-javascript-du1084lyn)
Last week you learned about making API calls. You learned how to do this using the XHR object, which we can access through the browser's `window` object.
59
96
60
-
As we've learned in the previous sections, JavaScript as a language evolves. But so do browsers!
97
+
Now as we've learned in the previous sections, JavaScript as a language evolves continually. But so do browsers! New features get added to increase the user experience and make life easier for developers.
98
+
99
+
One of those features added to browsers is an upgraded version of the XHR object. It's called `fetch` and it's the modern way to make API calls. It incorporates Promises, making it easier to handle your server responses.
100
+
101
+
A `fetch` function is now provided in the global `window` scope in the browser. You can check it out by opening your developers tools and searching for `fetch`. Keep in mind that this only counts for certain browser version. To figure out which browsers can use fetch, check [this](https://caniuse.com/#feat=fetch) out.
61
102
62
-
It's a modern way to make API calls. It incorporates Promises. A `fetch` function is now provided in the global `window` scope in the browser. You can check it out by opening your developers tools and searching for `fetch`.
103
+
Learn more about `fetch`:
63
104
105
+
-[Fetch API Introduction](https://www.youtube.com/watch?v=Oive66jrwBs)
64
106
-[Fetch() - Working with Data and APIs in JavaScript](https://www.youtube.com/watch?v=tc8DU14qX6I)
Copy file name to clipboardExpand all lines: Week3/README.md
+72-11Lines changed: 72 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -61,15 +61,25 @@ Almost anything that evolves within programming does so to solve a certain probl
61
61
62
62
The problems that OOP tries to solve is the question we saw before: how can we write code in an organized and reusable fashion?
63
63
64
-
The answer that OOP gives us can be summarised in the following 4 pillars:
64
+
The answer that OOP gives us can be summarised in 4 pillars. To illustrate these pillars let's use a simple example:
65
65
66
-
1.**Encapsulation**: bundling data and operations (that manipulate that data) together, and data hiding. Putting objects inside of a 'capsule' we can prevent direct manipulation by outside sources. Reducing dependencies between objects, so that change in one place doesn't affect the rest of the application.
66
+
```js
67
+
constperson= {
68
+
name:'Mohammad',
69
+
age:28,
70
+
walk() {
71
+
return`${this.name} is walking!`;
72
+
},
73
+
};
74
+
```
75
+
76
+
1.**Encapsulation**: In the example data and operations (that manipulate that data)are grouped together. This is called `encapsulation`. The main benefit is that this keeps our code organised. The second benefit is that putting objects inside of a 'capsule' we can prevent direct manipulation by outside sources, this is called `data hiding`. This reduces dependencies between objects, so that change in one place doesn't affect the rest of the application.
67
77
68
-
2.**Abstraction**: Complexity of logic hidden away, creating a simpler interface (remote controller to a tv). Only expose the essentials. Abstracting away complexities to create a
78
+
2.**Abstraction**: Let's say we had a Complexity of logic hidden away, creating a simpler interface (remote controller to a tv). Only expose the essentials. Abstracting away complexities to create an easier to use element.
69
79
70
80
3.**Inheritance**: eliminates redundant code by inheriting properties and methods in new instances. This encourages code reusability.
71
81
72
-
4.**Polymorphism**: an object can have many forms of expression, depending on the context.
82
+
4.**Polymorphism**: an object can have many forms of expression, depending on the context. Let's say we inherit
73
83
74
84
Any class that exists or is made follows these pillars.
75
85
@@ -97,31 +107,82 @@ const anObj = {
97
107
};
98
108
```
99
109
100
-
This is called an `object literal`, and it's a valid way of creating an object. However, writing it like this abstracts away a lot of what's happening behind the scenes.
110
+
This is called an `object literal`, and it's a valid way of creating an object. However, writing it like this "abstracts away" a lot of what's happening behind the scenes.
111
+
112
+
> To abstract away refers to intentionally hiding the details of how something complex works in order to simplify things conceptually. For example, the remote to your television is a complex device, but all of this is abstracted away so you don't have to deal with it. You just press the ON button and it works.
113
+
114
+
You can write the same thing by using the Object `constructor function`.
115
+
116
+
```
117
+
118
+
```
101
119
102
-
The same thing
120
+
Well what is a constructor function ? Let's start at the beginning: `factory functions`.
103
121
104
122
### Factory functions
105
123
106
-
What if we want to create hundreds of object instances?
124
+
If we want to create an object we can just use an `object literal` and we're done. But what if we want to create hundreds of object instances of that same object?
125
+
126
+
For that we use `factory functions`. Don't let the name mislead you though, a factory function is just a regular function. However, the single differentiating factor is that it always returns an object instance: it is a factory that produces object instances, hence the name `factory function`. Here's an example:
127
+
128
+
```js
129
+
// Defining a blueprint for a person:
130
+
functioncreatePerson(name, age) {
131
+
var obj = {
132
+
name: name,
133
+
age: age,
134
+
walk:function() {
135
+
console.log(`${this.name} is walking!`);
136
+
},
137
+
};
138
+
// other code to manipulate our object in some way here
139
+
return obj;
140
+
}
141
+
```
142
+
143
+
This is the most simple way of defining a `class` and creating object instances from it. Now every time we call this function we're creating a new person object.
144
+
145
+
```js
146
+
constnoer=createPerson('Noer', 27);
147
+
constwouter=createPerson('Wouter', 33);
148
+
constfederico=createPerson('Federico', 32);
149
+
```
150
+
151
+
Go through the following to learn more about factory functions:
`Constructor functions` are ordinary functions that have a special purpose: to create object instances. You can consider them the more advanced version of `factory functions`.
159
+
160
+
Here's an example:
161
+
162
+
```js
163
+
// // Defining a blueprint for a person:
164
+
functionPerson(name, age) {
165
+
this.name= name;
166
+
this.age= age;
167
+
}
168
+
```
169
+
170
+
The difference with a factory function is the way to instantiate it. Instead of just calling it we have to use the keyword `new`, like so:
-[Constructors and object instances](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS#Constructors_and_object_instances)
115
178
116
179
### Classes
117
180
118
-
Like mentioned before, a `class` is a blueprint/template that represents a real-world entity. Whenever we want to create a version of this class, we `instantiate` it: in other words, we're creating a object.
119
-
120
181
As you've learned in the previous section, in JavaScript we can do this using `factory/constructor function`.
121
182
122
-
Since ES6 we can
183
+
Since ES6 we can make use of the `class` keyword, which is a way to create constructor objects as well. It's essentially the same thing as a constructor function, only written in a clearer and more straightforward way.
123
184
124
-
It's essentially the same thing as a constructor function, only written in a clearer and more straightforward way.
0 commit comments