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/04-object-basics/03-symbol/article.md
+21-11Lines changed: 21 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,11 @@
3
3
4
4
By specification, object property keys may be either of string type, or of symbol type. Not numbers, not booleans, only strings or symbols, these two types.
5
5
6
-
Till now we've only seen strings. Now let's see the advantages that symbols can give us.
6
+
Till now we've been using only strings. Now let's see the benefits that symbols can give us.
7
7
8
8
## Symbols
9
9
10
-
"Symbol" value represents a unique identifier.
10
+
A "symbol" represents a unique identifier.
11
11
12
12
A value of this type can be created using `Symbol()`:
13
13
@@ -52,15 +52,15 @@ alert(id); // TypeError: Cannot convert a Symbol value to a string
52
52
53
53
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another.
54
54
55
-
If we really want to show a symbol, we need to call `.toString()` on it, like here:
55
+
If we really want to show a symbol, we need to explicitly call `.toString()` on it, like here:
56
56
```js run
57
57
let id = Symbol("id");
58
58
*!*
59
59
alert(id.toString()); // Symbol(id), now it works
60
60
*/!*
61
61
```
62
62
63
-
Or get `symbol.description` property to get the description only:
63
+
Or get `symbol.description` property to show the description only:
64
64
```js run
65
65
let id = Symbol("id");
66
66
*!*
@@ -74,13 +74,23 @@ alert(id.description); // id
74
74
75
75
Symbols allow us to create "hidden" properties of an object, that no other part of code can occasionally access or overwrite.
76
76
77
+
<<<<<<< HEAD
77
78
For instance, if we want to store an "identifier" for the object `user`, we can use a symbol as a key for it:
79
+
=======
80
+
For instance, if we're working with `user` objects, that belong to a third-party code. We'd like to add identifiers to them.
81
+
82
+
Let's use a symbol key for it:
83
+
>>>>>>> 852ee189170d9022f67ab6d387aeae76810b5923
78
84
79
85
```js run
80
-
let user = { name:"John" };
86
+
let user = { // belongs to another code
87
+
name:"John"
88
+
};
89
+
81
90
let id =Symbol("id");
82
91
83
-
user[id] ="ID Value";
92
+
user[id] =1;
93
+
84
94
alert( user[id] ); // we can access the data using the symbol as the key
85
95
```
86
96
@@ -106,13 +116,13 @@ Now note that if we used a string `"id"` instead of a symbol for the same purpos
106
116
```js run
107
117
let user = { name:"John" };
108
118
109
-
//our script uses "id" property
110
-
user.id="ID Value";
119
+
//Our script uses "id" property
120
+
user.id="Our id value";
111
121
112
-
// ...if later another script the uses "id" for its purposes...
122
+
// ...Another script also wants "id" for its purposes...
113
123
114
124
user.id="Their id value"
115
-
//boom! overwritten! it did not mean to harm the colleague, but did it!
Copy file name to clipboardExpand all lines: 1-js/09-classes/07-mixins/article.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,17 @@
2
2
3
3
In JavaScript we can only inherit from a single object. There can be only one `[[Prototype]]` for an object. And a class may extend only one other class.
4
4
5
+
<<<<<<< HEAD
5
6
But sometimes that feels limiting. For instance, I have a class `StreetSweeper` and a class `Bicycle`, and want to make a `StreetSweepingBicycle`.
7
+
=======
8
+
But sometimes that feels limiting. For instance, we have a class `StreetSweeper` and a class `Bicycle`, and want to make their mix: a `StreetSweepingBicycle`.
9
+
>>>>>>> 852ee189170d9022f67ab6d387aeae76810b5923
6
10
7
11
Or, talking about programming, we have a class `Renderer` that implements templating and a class `EventEmitter` that implements event handling, and want to merge these functionalities together with a class `Page`, to make a page that can use templates and emit events.
8
12
9
13
There's a concept that can help here, called "mixins".
10
14
11
-
As defined in Wikipedia, a [mixin](https://en.wikipedia.org/wiki/Mixin) is a class that contains methods for use by other classes without having to be the parent class of those other classes.
15
+
As defined in Wikipedia, a [mixin](https://en.wikipedia.org/wiki/Mixin) is a class containing methods that can be used by other classes without a need to inherit from it.
12
16
13
17
In other words, a *mixin* provides methods that implement a certain behavior, but we do not use it alone, we use it to add the behavior to other classes.
Copy file name to clipboardExpand all lines: 1-js/11-async/04-promise-error-handling/article.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,7 +150,11 @@ new Promise((resolve, reject) => {
150
150
}
151
151
152
152
}).then(function() {
153
+
<<<<<<<HEAD
153
154
/* never runs here */
155
+
=======
156
+
/* doesn't run here */
157
+
>>>>>>>852ee189170d9022f67ab6d387aeae76810b5923
154
158
}).catch(error=> { // (**)
155
159
156
160
alert(`The unknown error has occurred: ${error}`);
@@ -266,7 +270,11 @@ new Promise(function() {
266
270
267
271
In case of an error, the promise state becomes "rejected", and the execution should jump to the closest rejection handler. But there is no such handler in the examples above. So the error gets "stuck".
268
272
273
+
<<<<<<< HEAD
269
274
In practice, just like with a regular unhandled errors, it means that something has terribly gone wrong, the script probably died.
275
+
=======
276
+
In practice, just like with regular unhandled errors in code, it means that something has terribly gone wrong.
277
+
>>>>>>> 852ee189170d9022f67ab6d387aeae76810b5923
270
278
271
279
Most JavaScript engines track such situations and generate a global error in that case. We can see it in the console.
0 commit comments