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: 1-js/09-classes/07-mixins/article.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,13 +109,13 @@ As `super` looks for parent methods in `[[HomeObject]].[[Prototype]]`, that mean
109
109
110
110
Now let's make a mixin for real life.
111
111
112
-
An important feature of many browser objects (for instance) is that they can generate events. Events are a great way to "broadcast information" to anyone who wants it. So let's make a mixin that allows to easily add event-related functions to any class/object.
112
+
An important feature of many browser objects (for instance) is that they can generate events. Events are a great way to "broadcast information" to anyone who wants it. So let's make a mixin that allows us to easily add event-related functions to any class/object.
113
113
114
114
- The mixin will provide a method `.trigger(name, [...data])` to "generate an event" when something important happens to it. The `name` argument is a name of the event, optionally followed by additional arguments with event data.
115
115
- Also the method `.on(name, handler)` that adds `handler` function as the listener to events with the given name. It will be called when an event with the given `name` triggers, and get the arguments from `.trigger` call.
116
-
- ...And the method `.off(name, handler)` that removes `handler` listener.
116
+
- ...And the method `.off(name, handler)` that removes the `handler` listener.
117
117
118
-
After adding the mixin, an object `user` will become able to generate an event `"login"` when the visitor logs in. And another object, say, `calendar` may want to listen to such events to load the calendar for the logged-in person.
118
+
After adding the mixin, an object `user` will be able to generate an event `"login"` when the visitor logs in. And another object, say, `calendar` may want to listen for such events to load the calendar for the logged-in person.
119
119
120
120
Or, a `menu` can generate the event `"select"` when a menu item is selected, and other objects may assign handlers to react on that event. And so on.
121
121
@@ -165,7 +165,7 @@ let eventMixin = {
165
165
```
166
166
167
167
168
-
-`.on(eventName, handler)` -- assigns function `handler` to run when the event with that name happens. Technically, there's `_eventHandlers` property, that stores an array of handlers for each event name. So it just adds it to the list.
168
+
-`.on(eventName, handler)` -- assigns function `handler` to run when the event with that name occurs. Technically, there's an `_eventHandlers` property that stores an array of handlers for each event name, and it just adds it to the list.
169
169
-`.off(eventName, handler)` -- removes the function from the handlers list.
170
170
-`.trigger(eventName, ...args)` -- generates the event: all handlers from `_eventHandlers[eventName]` are called, with a list of arguments `...args`.
171
171
@@ -193,7 +193,7 @@ menu.on("select", value => alert(`Value selected: ${value}`));
193
193
menu.choose("123");
194
194
```
195
195
196
-
Now if we'd like any code to react on menu selection, we can listen to it with `menu.on(...)`.
196
+
Now, if we'd like any code to react to a menu selection, we can listen for it with `menu.on(...)`.
197
197
198
198
And `eventMixin` mixin makes it easy to add such behavior to as many classes as we'd like, without interfering with the inheritance chain.
199
199
@@ -203,6 +203,6 @@ And `eventMixin` mixin makes it easy to add such behavior to as many classes as
203
203
204
204
Some other languages allow multiple inheritance. JavaScript does not support multiple inheritance, but mixins can be implemented by copying methods into prototype.
205
205
206
-
We can use mixins as a way to augment a class by multiple behaviors, like event-handling as we have seen above.
206
+
We can use mixins as a way to augment a class by adding multiple behaviors, like event-handling as we have seen above.
207
207
208
-
Mixins may become a point of conflict if they accidentally overwrite existing class methods. So generally one should think well about the naming methods of a mixin, to minimize the probability of that.
208
+
Mixins may become a point of conflict if they accidentally overwrite existing class methods. So generally one should think well about the naming methods of a mixin, to minimize the probability of that happening.
Copy file name to clipboardExpand all lines: 1-js/10-error-handling/1-try-catch/article.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Error handling, "try..catch"
2
2
3
-
No matter how great we are at programming, sometimes our scripts have errors. They may occur because of our mistakes, an unexpected user input, an erroneous server response and for a thousand other reasons.
3
+
No matter how great we are at programming, sometimes our scripts have errors. They may occur because of our mistakes, an unexpected user input, an erroneous server response, and for a thousand other reasons.
4
4
5
5
Usually, a script "dies" (immediately stops) in case of an error, printing it to console.
6
6
@@ -25,8 +25,8 @@ try {
25
25
It works like this:
26
26
27
27
1. First, the code in `try {...}` is executed.
28
-
2. If there were no errors, then `catch(err)` is ignored: the execution reaches the end of `try` and goes on skipping `catch`.
29
-
3. If an error occurs, then `try` execution is stopped, and the control flows to the beginning of `catch(err)`. The `err` variable (can use any name for it) contains an error object with details about what's happened.
28
+
2. If there were no errors, then `catch(err)` is ignored: the execution reaches the end of `try` and goes on, skipping `catch`.
29
+
3. If an error occurs, then `try` execution is stopped, and the control flows to the beginning of `catch(err)`. The `err` variable (can use any name for it) will contain an error object with details about what happened.
30
30
31
31

32
32
@@ -668,4 +668,4 @@ We can also generate our own errors using the `throw` operator. Technically, the
668
668
669
669
*Rethrowing* is a very important pattern of error handling: a `catch` block usually expects and knows how to handle the particular error type, so it should rethrow errors it doesn't know.
670
670
671
-
Even if we don't have `try..catch`, most environments allow to setup a "global" error handler to catch errors that "fall out". In-browser that's `window.onerror`.
671
+
Even if we don't have `try..catch`, most environments allow us to setup a "global" error handler to catch errors that "fall out". In-browser, that's `window.onerror`.
Copy file name to clipboardExpand all lines: 1-js/10-error-handling/2-custom-errors/article.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ Our function `readUser(json)` will not only read JSON, but check ("validate") th
23
23
24
24
Our `ValidationError` class should inherit from the built-in `Error` class.
25
25
26
-
That class is built-in, here's it approximate code, for us to understand what we're extending:
26
+
That class is built-in, but here's its approximate code so we can understand what we're extending:
27
27
28
28
```js
29
29
// The "pseudocode" for the built-in Error class defined by JavaScript itself
@@ -215,9 +215,9 @@ Now custom errors are much shorter, especially `ValidationError`, as we got rid
215
215
216
216
The purpose of the function `readUser` in the code above is "to read the user data". There may occur different kinds of errors in the process. Right now we have `SyntaxError` and `ValidationError`, but in the future `readUser` function may grow and probably generate other kinds of errors.
217
217
218
-
The code which calls `readUser` should handle these errors. Right now it uses multiple `if` in the `catch` block, that check the class and handle known errors and rethrow the unknown ones. But if `readUser` function generates several kinds of errors -- then we should ask ourselves: do we really want to check for all error types one-by-one in every code that calls `readUser`?
218
+
The code which calls `readUser` should handle these errors. Right now it uses multiple `if`s in the `catch` block, that check the class and handle known errors and rethrow the unknown ones. But if the `readUser` function generates several kinds of errors, then we should ask ourselves: do we really want to check for all error types one-by-one in every code that calls `readUser`?
219
219
220
-
Often the answer is "No": the outer code wants to be "one level above all that". It wants to have some kind of "data reading error". Why exactly it happened -- is often irrelevant (the error message describes it). Or, even better if there is a way to get error details, but only if we need to.
220
+
Often the answer is "No": the outer code wants to be "one level above all that", it just wants to have some kind of "data reading error" -- why exactly it happened is often irrelevant (the error message describes it). Or, even better, it could have a way to get the error details, but only if we need to.
221
221
222
222
So let's make a new class `ReadError` to represent such errors. If an error occurs inside `readUser`, we'll catch it there and generate `ReadError`. We'll also keep the reference to the original error in its `cause` property. Then the outer code will only have to check for `ReadError`.
Copy file name to clipboardExpand all lines: 1-js/11-async/03-promise-chaining/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -218,7 +218,7 @@ new Promise(resolve => resolve(1))
218
218
219
219
JavaScript checks the object returned by `.then` handler in the line `(*)`: if it has a callable method named `then`, then it calls that method providing native functions `resolve`, `reject` as arguments (similar to executor) and waits until one of them is called. In the example above `resolve(2)` is called after 1 second `(**)`. Then the result is passed further down the chain.
220
220
221
-
This feature allows to integrate custom objects with promise chains without having to inherit from `Promise`.
221
+
This feature allows us to integrate custom objects with promise chains without having to inherit from `Promise`.
0 commit comments