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
{{ message }}
This repository was archived by the owner on May 14, 2024. It is now read-only.
@@ -62,7 +63,47 @@ A big part of what applications do is **moving data from one place to another**.
62
63
63
64
It's because the HackYourFuture website sends a **HTTP Request** to Stripe. The request basically says "Hey Stripe, some user from the HackYourFuture site wants to make a digital payment, can you handle that?". As a response Stripe answers "Of course, send the user to this specific URL and I'll take it from there!".
64
65
65
-
> Anytime a request to an API is made this is called a `HTTP Request`. However, in practice people use different terms for the same thing. Synonyms for `HTTP Request` are `API call/request`, `Network call/request` or`HTTP call`. Which do you prefer?
66
+
> Anytime a request to an API is made this is called a `HTTP Request`. However, in practice people use different terms for the same thing. Synonyms for `HTTP Request` are `API call/request`, `Network call/request`, `Web request/call` or`HTTP call`. Which do you prefer?
67
+
68
+
A HTTP Request has to be made using a special method. The browser gives us two of them: `XMLHttpRequest` and `Fetch API`. `XMLHttpRequest` (or XHR for short) is the older, more verbose method. It looks like this:
69
+
70
+
```js
71
+
// 1. Create a new XMLHttpRequest object
72
+
constxhr=newXMLHttpRequest();
73
+
74
+
// 2. Configure it: GET-request for the URL /article/.../load
// 4. This will be called after the response is received
81
+
xhr.onload=function () {
82
+
if (xhr.status!=200) {
83
+
// analyze HTTP status of the response
84
+
alert(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
85
+
} else {
86
+
// show the result
87
+
alert(`Done, got ${xhr.response.length} bytes`); // response is the server
88
+
}
89
+
};
90
+
91
+
xhr.onprogress=function (event) {
92
+
if (event.lengthComputable) {
93
+
alert(`Received ${event.loaded} of ${event.total} bytes`);
94
+
} else {
95
+
alert(`Received ${event.loaded} bytes`); // no Content-Length
96
+
}
97
+
};
98
+
99
+
xhr.onerror=function () {
100
+
alert('Request failed');
101
+
};
102
+
```
103
+
104
+
This way of making HTTP Requests is outdated (and not recommended to use), but it's good to be aware of it as you might still see it in old code bases.
105
+
106
+
The newer way of making HTTP Requests involves using the `Fetch API`. You'll learn more about that next week!
66
107
67
108
For further study of how to make HTTP Requests, check out the following resources:
68
109
@@ -80,32 +121,70 @@ The term is an acronym for `asynchronous JavaScript and XML`. Let's pick that ap
80
121
81
122
This technique was used back in the days when the web wasn't that advanced. Back then we used XML is the standard format we used to structure our data in. Nowadays we have replaced it with another data format: `JSON`.
82
123
83
-
### JSON
124
+
### JavaScript Object Notation (JSON)
84
125
85
126
`JSON` stands for JavaScript Object Notation and is a very JavaScript-like data format. Here's a small example:
86
127
87
128
```json
88
129
{
89
-
"first name": "John",
90
-
"last name": "Smith",
91
-
"age": 25,
130
+
"first name": "Noer",
131
+
"last name": "Paanakker",
132
+
"age": 28,
92
133
"address": {
93
-
"street address": "21 2nd Street",
94
-
"city": "New York",
95
-
"state": "NY",
96
-
"postal code": "10021"
134
+
"street address": "Strekkerweg 79",
135
+
"city": "Amsterdam",
136
+
"postal code": "1033 DA"
97
137
}
98
138
}
99
139
```
100
140
101
141
If you look closely it almost looks exactly like a regular JavaScript object. There are 2 big differences: (1) in a JSON object everything is turned into a string (als known as "stringified"), and (2) it's not tied to the JavaScript language. Actually, many other languages can work with JSON!
102
142
103
-
In AJAX we make a HTTP Request to a web server, that in response sends us back information to be used in the frontend. Generally speaking, this data will be send in `JSON` format.
143
+
In AJAX we make a HTTP Request to a web server, that then responds back with information to be used in the frontend. Generally speaking, this data will be send in `JSON` format. The web server "stringifies" (makes into a string) the data to be send first before it sends it.
144
+
145
+
### Stringifying and parsing JSON
104
146
105
-
So, technically speaking, the term would actually be AJAJ. However, the industry has decided to stick with the term AJAX to refer to these processes.
147
+
JSON is the modern web standard data format to send and receive data in. In order to make something into JSON format we need to `stringify` it: make the whole object into one string. Luckily, JavaScript gives us a way to do this:
106
148
107
-
Go through the following to learn about how to use JSON and AJAX practically:
Here's another way of looking at the "stringifying" process: let's say you want to send your mother a gift, a brand new HackYourFuture T-shirt. Would you just put the shirt right into the mailbox, like that? Of course not! You would wrap it up nicely and put it into a box. Then you put it in the mailbox and off it goes!
161
+
162
+
This act of putting something into a box is what's happening when we `stringify` data (either on the client-side or server-side).
163
+
164
+
After the JSON data has been send, the receiver has to be able to interpret it. This process of making JSON interpretable by the programming language within that environment is called `parsing`. As we're using JavaScript, it doesn't seem like a big stretch. But what if we're using some other programming language like Python or Java?
165
+
166
+
To follow our analogy, this is basically your mother unpacking her T-shirt from out of the box you put it in!
167
+
168
+
Again, in JavaScript we can use another method gained from the global `JSON` object in order to `parse` our JSON data:
Nowadays we use JSON to perform asynchronous operations using JavaScript. So, technically speaking, the term would actually be AJAJ. However, the industry has decided to stick with the term AJAX to refer to these processes. Keep that in mind whenever someone asks you about it!
184
+
185
+
Go through the following to learn more about JSON and AJAX:
@@ -115,7 +194,7 @@ Traditionally, in order to make use of the AJAX technique we need to make use of
115
194
116
195
> The `window` object is the most top-level object available to us in the browser. It contains the `document`, which contains all the HTML/CSS and JavaScript we write. Besides this, the `window` also contains a lot of other things we use when writing frontend code: `setTimeout()`, `alert()` and it even contains a reference to the `console` (from which we get `console.log()`). Try it out in the console if you want to see for yourself!
117
196
118
-
By creating a new instance of this object we can start making AJAX requests!
197
+
By creating a new instance of this object we can start making HTTP requests!
119
198
120
199
```js
121
200
constxhr=newXMLHttpRequest();
@@ -135,31 +214,42 @@ Check the following resources to learn more about XHR.
135
214
136
215
### What's a module?
137
216
138
-
A `module` is a part of a program that contains one or more functionalities. For example, a single function that has only 1 job could be considered a module.
217
+
A `module` is a part of an application that contains usually a single functionality. For example, a single function that has only 1 job could be considered a module. For example:
218
+
219
+
```js
220
+
functionaddNums(num1, num2) {
221
+
return num1 + num2;
222
+
}
223
+
```
224
+
225
+
If this little function has its own dedicated `.js` file and you can import it into another file, it's a module!
139
226
140
-
When developing applications you'll always be writing multiple functionalities in order for your software to work as expected. These can be written all in one file, and it would fine. The browser/operating system would be able to interpret and execute it anyway. But for you, the human, it's very hard to keep overview of what is happening at what level of the application.
227
+
When developing applications you'll always be writing multiple functionalities in order for your software to work as expected. These can be written all in one file, and that would still work. The browser/operating system would be able to interpret and execute it anyway. But for you, the human, it's very hard to keep overview of what is happening at what level of the application. Can you only imagine having to look through one big file of 1000's of lines of code, just to find
141
228
142
-
In order to keep a better overview, we can choose to **modularize** our application: split it up into smaller parts that, in theory, all work independently.
229
+
In order to keep a better overview, we can choose to **modularize** our application. This means: splitting it up into smaller parts (modules) that, in theory, all work independently.
143
230
144
231
However, creating better overview is not the only reason. Among other reasons, modules make a developer's job easy by:
145
232
146
-
-Allowing them to focus on only one area of the functionality of the software application
233
+
-Making the application easier to maintain, by making it more readable and thus easier to modify
147
234
- Isolating individual blocks of code, in order to make errors more easily traceable
148
235
- Encouraging the developer to write code in a way that makes it reusable
149
236
150
237
For more information about this, go through the following:
151
238
239
+
-[Introduction to Modular Design](https://www.youtube.com/watch?v=20JP8w6_nVA)
240
+
-[JavaScript Patterns: The Traditional Module Pattern](https://www.youtube.com/watch?v=SKBmJ9P6OAk)
241
+
-[JavaScript Modules in 100 Seconds](youtube.com/watch?v=qgRUr-YUk1Q)
152
242
-[JavaScript Modules: From IIFEs to CommonJS to ES6 Modules](https://www.youtube.com/watch?v=qJWALEoGge4)
153
243
154
244
### What's a library?
155
245
156
246
If you've ever written code you know how easy it is to duplicate it: you just copy and paste it.
157
247
158
-
Modules are small blocks of code that make up a functionality. But what if you have a bunch of modules that collectively aim to solve a bigger problem, like creating a [Single Page Application](https://en.wikipedia.org/wiki/Single-page_application)?
248
+
Modules are small blocks of code that make up a functionality. But what if you have a bunch of modules that collectively aim to solve a bigger problem, like creating [data visualizations](https://d3js.org/) or make DOM manipulation easier ([jQuery](https://jquery.com/))?
159
249
160
-
For this we use a `library`: code that a developer (or a team of developers) has written in order to solve these bigger problems within an application. A library, typically, contains a collection of modules that work together in order to solve a bigger problem. Examples of big problems that require libraries are validation ([validator.js](https://www.npmjs.com/package/validator)), or quick DOM manipluation ([jQuery](https://www.npmjs.com/package/jquery)).
250
+
For this we use a `library`: code that a developer (or a team of developers) has written in order to solve these bigger problems within an application. A library, typically, contains a collection of modules that work together in order to solve a bigger problem.
161
251
162
-
> Like many things in programming, people use various terms to describe the same thing. In the case of `library`, you'll often hear it spoken of as `package`or `namespace`.
252
+
> Like many things in programming, people use various terms to describe the same thing. In the case of `library`, you'll often hear it spoken of as `package`, `namespace`or `dependency`.
163
253
164
254
Why do we use libraries? We use them to help us make building applications easier. Think of it like building a house: in theory you could do it all by hand. But as you can imagine, this is highly inefficient and time-consuming. So instead we use tools to help us out. These can be small tools (like a hammer or screwdriver) or bigger ones (like a concrete mixer or wheel barrow).
165
255
@@ -182,10 +272,11 @@ For further study, check the following:
In a previous section we discussed APIs and the importance of being able to make HTTP Requests. We have seen that we can use the XHR object to do so. In this section we'll discuss a `library` that makes this process easier for us. It's called [axios](https://github.com/axios/axios), a JavaScript library that allows us to make HTTP Requests in an easier way.
279
+
In a previous section we discussed APIs and the importance of being able to make HTTP Requests so that we can communicate with them. We have seen that we can use the `XHR` object to do so. In this section we'll discuss a `library` that makes this process easier for us. It's called [axios](https://github.com/axios/axios), a JavaScript library that allows us to make HTTP Requests in an easier way.
Copy file name to clipboardExpand all lines: Week2/README.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ These are the topics for week 2:
16
16
17
17
You are undoubtedly different than when you were a baby. Back then you couldn't do much except crying. That's pretty much it. But as the years pass you increasingly could do more and more: walking, socializing or playing an instrument.
18
18
19
-
Likewise, so has JavaScript evolved. Throughout the course you have, unknowingly, used syntax from different JavaScript versions. For example, if you've ever declared a function like this:
19
+
Likewise, so has JavaScript evolved. Throughout the HackYourFuture course you have, unknowingly, used syntax from different JavaScript versions. For example, if you've ever declared a function like this:
20
20
21
21
```js
22
22
functionaFunction() {
@@ -120,6 +120,7 @@ A `fetch` function is now provided in the global `window` scope in the browser.
120
120
Learn more about `fetch`:
121
121
122
122
-[Fetch API Introduction](https://www.youtube.com/watch?v=Oive66jrwBs)
123
+
-[Sending JavaScript Http Requests with the fetch() API](https://www.youtube.com/watch?v=23hrM4saaMk)
123
124
-[Fetch() - Working with Data and APIs in JavaScript](https://www.youtube.com/watch?v=tc8DU14qX6I)
0 commit comments