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
+16-12Lines changed: 16 additions & 12 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,15 +74,19 @@ 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
-
For instance, if we're working with `user` objects, that belong to a third-party code and don't have any `id` field. We'd like to add identifiers to them.
77
+
For instance, if we're working with `user` objects, that belong to a third-party code. We'd like to add identifiers to them.
78
78
79
79
Let's use a symbol key for it:
80
80
81
81
```js run
82
-
let user = { name:"John" };
82
+
let user = { // belongs to another code
83
+
name:"John"
84
+
};
85
+
83
86
let id =Symbol("id");
84
87
85
-
user[id] ="ID Value";
88
+
user[id] =1;
89
+
86
90
alert( user[id] ); // we can access the data using the symbol as the key
87
91
```
88
92
@@ -108,13 +112,13 @@ There will be no conflict between our and their identifiers, because symbols are
108
112
```js run
109
113
let user = { name:"John" };
110
114
111
-
//our script uses "id" property
112
-
user.id="ID Value";
115
+
//Our script uses "id" property
116
+
user.id="Our id value";
113
117
114
-
// ...if later another script the uses "id" for its purposes...
118
+
// ...Another script also wants "id" for its purposes...
115
119
116
120
user.id="Their id value"
117
-
//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
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
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
-
But sometimes that feels limiting. For instance, I have a class `StreetSweeper` and a class `Bicycle`, and want to make their mix: a `StreetSweepingBicycle`.
5
+
But sometimes that feels limiting. For instance, we have a class `StreetSweeper` and a class `Bicycle`, and want to make their mix: a `StreetSweepingBicycle`.
6
6
7
7
Or we have a class `User` and a class `EventEmitter` that implements event generation, and we'd like to add the functionality of `EventEmitter` to `User`, so that our users can emit events.
8
8
9
9
There's a concept that can help here, called "mixins".
10
10
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.
11
+
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
12
13
13
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/99-js-misc/03-currying-partials/article.md
+15-8Lines changed: 15 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ As you can see, the implementation is straightforward: it's just two wrappers.
40
40
41
41
- The result of `curry(func)` is a wrapper `function(a)`.
42
42
- When it is called like `sum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned `function(b)`.
43
-
- Then `sum(1)(2)` finally calls `function(b)` providing`2`, and it passes the call to the original multi-argument`sum`.
43
+
- Then this wrapper is called with`2` as an argument, and it passes the call to the original `sum`.
44
44
45
45
More advanced implementations of currying, such as [_.curry](https://lodash.com/docs#curry) from lodash library, return a wrapper that allows a function to be called both normally and partially:
46
46
@@ -73,10 +73,15 @@ Let's curry it!
73
73
log =_.curry(log);
74
74
```
75
75
76
-
After that `log` work both the normal way and in the curried form:
76
+
After that `log` work normally:
77
+
78
+
```js
79
+
log(newDate(), "DEBUG", "some debug"); // log(a, b, c)
The new `curry` may look complicated, but it's actually easy to understand.
144
149
145
-
The result of `curry(func)` is the wrapper `curried` that looks like this:
150
+
The result of `curry(func)`call is the wrapper `curried` that looks like this:
146
151
147
152
```js
148
153
// func is the function to transform
@@ -157,7 +162,7 @@ function curried(...args) {
157
162
};
158
163
```
159
164
160
-
When we run it, there are two execution branches:
165
+
When we run it, there are two `if`execution branches:
161
166
162
167
1. Call now: if passed `args` count is the same as the original function has in its definition (`func.length`) or longer, then just pass the call to it.
163
168
2. Get a partial: otherwise, `func` is not called yet. Instead, another wrapper `pass` is returned, that will re-apply `curried` providing previous arguments together with the new ones. Then on a new call, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
@@ -173,7 +178,9 @@ For the call `curried(1)(2)(3)`:
173
178
If that's still not obvious, just trace the calls sequence in your mind or on the paper.
174
179
175
180
```smart header="Fixed-length functions only"
176
-
The currying requires the function to have a known fixed number of arguments.
181
+
The currying requires the function to have a fixed number of arguments.
182
+
183
+
A function that uses rest parameters, such as `f(...args)`, can't be curried this way.
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/1-behavior-nested-tooltip/task.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
@@ -4,10 +4,12 @@ importance: 5
4
4
5
5
# Improved tooltip behavior
6
6
7
-
Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`.
7
+
Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`. The value of this attribute should become the tooltip text.
8
8
9
9
That's like the task <info:task/behavior-tooltip>, but here the annotated elements can be nested. The most deeply nested tooltip is shown.
10
10
11
+
Only one tooltip may show up at the same time.
12
+
11
13
For instance:
12
14
13
15
```html
@@ -21,5 +23,3 @@ For instance:
21
23
The result in iframe:
22
24
23
25
[iframe src="solution" height=300 border=1]
24
-
25
-
P.S. Hint: only one tooltip may show up at the same time.
0 commit comments