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/01-getting-started/2-code-editors/article.md
+17Lines changed: 17 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,7 @@ Un IDE încarcă proiectul (pot fi mai multe fișiere), permite navigarea între
12
12
13
13
Dacă nu ai selectat până acum un IDE, uită-te la următoarele variante:
14
14
15
+
<<<<<<< HEAD
15
16
-[WebStorm](http://www.jetbrains.com/webstorm/) pentru dezvoltare frontend și alte editoare ale aceleiași companii dacă ai nevoide de limbaje adiționale (plătit).
16
17
-[Netbeans](http://netbeans.org/) (plătit).
17
18
@@ -20,6 +21,14 @@ Toate IDE-urile sunt cross-platform.
20
21
Pentru Windows există de asemenea un editor "Visual Studio", a nu se confunda cu "Visual Studio Code". "Visual Studio" este un editor doar pentru Windows, plătit dar puternic, foarte potrivit pentru platforma .NET. O versiune gratuită al lui este [Visual Studio Community](https://www.visualstudio.com/vs/community/).
21
22
22
23
Multe IDE-uri sunt plătite dar au o perioadă de încercare. Costul lor este în mod normal neglijabil, în comparație cu salariul unui dezvoltator calificat, așa că doar alege unul potrivit pentru tine.
24
+
=======
25
+
-[Visual Studio Code](https://code.visualstudio.com/) (cross-platform, free).
For Windows, there's also "Visual Studio", not to be confused with "Visual Studio Code". "Visual Studio" is a paid and mighty Windows-only editor, well-suited for the .NET platform. It's also good at JavaScript. There's also a free version [Visual Studio Community](https://www.visualstudio.com/vs/community/).
29
+
30
+
Many IDEs are paid, but have a trial period. Their cost is usually negligible compared to a qualified developer's salary, so just choose the best one for you.
31
+
>>>>>>> 027933531e121650120f7e8385f691de99af12d2
23
32
24
33
## Editoare de categorie ușoară
25
34
@@ -33,12 +42,17 @@ Diferența majoră dintre un editor de "categorie ușoară" și un "IDE" este c
33
42
34
43
Următoarele opțiuni merită atenția ta:
35
44
45
+
<<<<<<< HEAD
36
46
-[Visual Studio Code](https://code.visualstudio.com/) (cross-platform, gratuit) de asemenea are multe feature-uri de tip IDE.
-[Vim](http://www.vim.org/) și [Emacs](https://www.gnu.org/software/emacs/) sunt de asemenea utile dacă știi cum să le folosești.
41
54
55
+
<<<<<<< HEAD
42
56
## Favoritele mele
43
57
44
58
Preferința personală a autorului este de a avea atât un IDE pentru proiecte cât și un editor de categorie ușoară pentru editare de fișiere rapidă și ușoară.
@@ -49,6 +63,9 @@ Eu folosesc:
49
63
- Pe post de editor de categorie ușoară -- [Sublime Text](http://www.sublimetext.com) sau [Atom](https://atom.io/).
50
64
51
65
## Să nu ne certăm
66
+
=======
67
+
## Let's not argue
68
+
>>>>>>> 027933531e121650120f7e8385f691de99af12d2
52
69
53
70
Editoarele din lista de mai sus sunt cele pe care fie eu sau prietenii mei, pe care îi consider dezvoltatori buni, le-am folosit pentru un timp îndelungat și suntem mulțumiți de ele.
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/03-symbol/article.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ A value of this type can be created using `Symbol()`:
16
16
let id =Symbol();
17
17
```
18
18
19
-
We can also give symbol a description (also called a symbol name), mostly useful for debugging purposes:
19
+
Upon creation, we can give symbol a description (also called a symbol name), mostly useful for debugging purposes:
20
20
21
21
```js run
22
22
// id is a symbol with the description "id"
@@ -74,7 +74,7 @@ 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 want to store an "identifier" for the object `user`, we can use a symbol as a key for it:
77
+
For instance, if we'd like to add an "identifier" to the object `user`, we can use a symbol as a key for it:
78
78
79
79
```js run
80
80
let user = { name:"John" };
@@ -88,7 +88,7 @@ What's the benefit of using `Symbol("id")` over a string `"id"`?
88
88
89
89
Let's make the example a bit deeper to see that.
90
90
91
-
Imagine that another script wants to have its own "id" property inside `user`, for its own purposes. That may be another JavaScript library, so the scripts are completely unaware of each other.
91
+
Imagine that another script wants to have its own identifier inside `user`, for its own purposes. That may be another JavaScript library, so thes scripts are completely unaware of each other.
92
92
93
93
Then that script can create its own `Symbol("id")`, like this:
94
94
@@ -99,9 +99,9 @@ let id = Symbol("id");
99
99
user[id] ="Their id value";
100
100
```
101
101
102
-
There will be no conflict, because symbols are always different, even if they have the same name.
102
+
There will be no conflict between our and their identifiers, because symbols are always different, even if they have the same name.
103
103
104
-
Now note that if we used a string `"id"` instead of a symbol for the same purpose, then there *would* be a conflict:
104
+
...But if we used a string `"id"` instead of a symbol for the same purpose, then there *would* be a conflict:
105
105
106
106
```js run
107
107
let user = { name:"John" };
@@ -117,7 +117,7 @@ user.id = "Their id value"
117
117
118
118
### Symbols in a literal
119
119
120
-
If we want to use a symbol in an object literal, we need square brackets.
120
+
If we want to use a symbol in an object literal`{...}`, we need square brackets around it.
121
121
122
122
Like this:
123
123
@@ -155,7 +155,7 @@ for (let key in user) alert(key); // name, age (no symbols)
155
155
alert( "Direct: "+ user[id] );
156
156
```
157
157
158
-
That's a part of the general "hiding" concept. If another script or a library loops over our object, it won't unexpectedly access a symbolic property.
158
+
`Object.keys(user)` also ignores them. That's a part of the general "hiding symbolic properties" principle. If another script or a library loops over our object, it won't unexpectedly access a symbolic property.
159
159
160
160
In contrast, [Object.assign](mdn:js/Object/assign) copies both string and symbol properties:
As we've seen, usually all symbols are different, even if they have the same names. But sometimes we want same-named symbols to be same entities.
193
+
As we've seen, usually all symbols are different, even if they have the same name. But sometimes we want same-named symbols to be same entities.
194
194
195
195
For instance, different parts of our application want to access symbol `"id"` meaning exactly the same property.
196
196
197
197
To achieve that, there exists a *global symbol registry*. We can create symbols in it and access them later, and it guarantees that repeated accesses by the same name return exactly the same symbol.
198
198
199
-
In order to create or read a symbol in the registry, use `Symbol.for(key)`.
199
+
In order to read (create if absent) a symbol from the registry, use `Symbol.for(key)`.
200
200
201
201
That call checks the global registry, and if there's a symbol described as `key`, then returns it, otherwise creates a new symbol `Symbol(key)` and stores it in the registry by the given `key`.
202
202
@@ -206,7 +206,7 @@ For instance:
206
206
// read from the global registry
207
207
let id =Symbol.for("id"); // if the symbol did not exist, it is created
208
208
209
-
// read it again
209
+
// read it again (maybe from another part of the code)
210
210
let idAgain =Symbol.for("id");
211
211
212
212
// the same symbol
@@ -266,14 +266,14 @@ Other symbols will also become familiar when we study the corresponding language
266
266
267
267
`Symbol` is a primitive type for unique identifiers.
268
268
269
-
Symbols are created with `Symbol()` call with an optional description.
269
+
Symbols are created with `Symbol()` call with an optional description (name).
270
270
271
-
Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry: `Symbol.for(key)` returns (creates if needed) a global symbol with `key` as the name. Multiple calls of `Symbol.for` return exactly the same symbol.
271
+
Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry: `Symbol.for(key)` returns (creates if needed) a global symbol with `key` as the name. Multiple calls of `Symbol.for`with the same `key`return exactly the same symbol.
272
272
273
273
Symbols have two main use cases:
274
274
275
275
1. "Hidden" object properties.
276
-
If we want to add a property into an object that "belongs" to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in `for..in`, so it won't be occasionally listed. Also it won't be accessed directly, because another script does not have our symbol, so it will not occasionally intervene into its actions.
276
+
If we want to add a property into an object that "belongs" to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in `for..in`, so it won't be occasionally processed together with other properties. Also it won't be accessed directly, because another script does not have our symbol. So the property will be protected from occasional use or overwrite.
277
277
278
278
So we can "covertly" hide something into objects that we need, but others should not see, using symbolic properties.
Using objects as keys is one of most notable and important `Map` features. For string keys, `Object` can be fine, but it would be difficult to replace the `Map` with a regular `Object` in the example above.
60
60
61
-
In the old times, before `Map` existed, people added unique identifiers to objects for that:
61
+
Let's try:
62
+
63
+
```js run
64
+
let john = { name:"John" };
65
+
66
+
let visitsCountObj = {}; // try to use an object
67
+
68
+
visitsCountObj[john] =123; // try to use john object as the key
As `john` is an object, it got converted to the key string `"[object Object]"`. All objects without a special conversion handling are converted to such string, so they'll all mess up.
77
+
78
+
In the old times, before `Map` existed, people used to add unique identifiers to objects for that:
62
79
63
80
```js run
64
81
// we add the id field
@@ -159,7 +176,7 @@ The iteration goes in the same order as the values were inserted. `Map` preserve
159
176
Besides that, `Map` has a built-in `forEach` method, similar to `Array`:
160
177
161
178
```js
162
-
// runs the function for each (key, value) pair
179
+
// runs the function for each (key, value) pair
163
180
recipeMap.forEach( (value, key, map) => {
164
181
alert(`${key}: ${value}`); // cucumber: 500 etc
165
182
});
@@ -172,7 +189,7 @@ A `Set` is a collection of values, where each value may occur only once.
172
189
173
190
Its main methods are:
174
191
175
-
- `new Set(iterable)` -- creates the set, optionally from an array of values (any iterable will do).
192
+
- `new Set(iterable)` -- creates the set, and if an `iterable` object is provided (usually an array), copies values from it into the set.
176
193
- `set.add(value)` -- adds a value, returns the set itself.
177
194
- `set.delete(value)` -- removes the value, returns `true` if `value` existed at the moment of the call, otherwise `false`.
178
195
- `set.has(value)` -- returns `true` if the value exists in the set, otherwise `false`.
Note the funny thing. The `forEach` function in the `Set` has 3 arguments: a value, then *again a value*, and then the target object. Indeed, the same value appears in the arguments twice.
242
+
Note the funny thing. The callback function passed in `forEach` has 3 arguments: a value, then *again a value*, and then the target object. Indeed, the same value appears in the arguments twice.
226
243
227
-
That's for compatibility with `Map` where `forEach` has three arguments. Looks a bit strange, for sure. But may help to replace `Map` with `Set` in certain cases with ease, and vice versa.
244
+
That's for compatibility with `Map` where the callback passed `forEach` has three arguments. Looks a bit strange, for sure. But may help to replace `Map` with `Set` in certain cases with ease, and vice versa.
228
245
229
246
The same methods `Map` has for iterators are also supported:
Copy file name to clipboardExpand all lines: 1-js/05-data-types/08-keys-values-entries/article.md
+90-5Lines changed: 90 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
2
2
# Object.keys, values, entries
3
3
4
-
Let's step away from the individual data structures and talk about the iterations over them.
4
+
Let's step away from the individual data structures and talk about the iterations over them.
5
5
6
6
In the previous chapter we saw methods `map.keys()`, `map.values()`, `map.entries()`.
7
7
8
-
These methods are generic, there is a common agreement to use them for data structures. If we ever create a data structure of our own, we should implement them too.
8
+
These methods are generic, there is a common agreement to use them for data structures. If we ever create a data structure of our own, we should implement them too.
9
9
10
10
They are supported for:
11
11
@@ -63,8 +63,93 @@ for (let value of Object.values(user)) {
Just like a `for..in` loop, these methods ignore properties that use `Symbol(...)` as keys.
69
68
70
-
Usually that's convenient. But if we want symbolic keys too, then there's a separate method [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) that returns an array of only symbolic keys. Also, the method [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) returns *all* keys.
69
+
Usually that's convenient. But if we want symbolic keys too, then there's a separate method [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) that returns an array of only symbolic keys. Also, there exist a method [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) that returns *all* keys.
70
+
```
71
+
72
+
## Object.fromEntries to transform objects
73
+
74
+
Sometimes we need to perform a transformation of an object to `Map` and back.
75
+
76
+
We already have `new Map(Object.entries(obj))` to make a `Map` from `obj`.
77
+
78
+
The syntax of `Object.fromEntries` does the reverse. Given an array of `[key, value]` pairs, it creates an object:
79
+
80
+
```js run
81
+
let prices =Object.fromEntries([
82
+
['banana', 1],
83
+
['orange', 2],
84
+
['meat', 4]
85
+
]);
86
+
87
+
// now prices = { banana: 1, orange: 2, meat: 4 }
88
+
89
+
alert(prices.orange); // 2
90
+
```
91
+
92
+
Let's see practical applications.
93
+
94
+
For example, we'd like to create a new object with double prices from the existing one.
95
+
96
+
For arrays, we have `.map` method that allows to transform an array, but nothing like that for objects.
...Or we can represent the object as an `Array` using `Object.entries`, then perform the operations with `map` (and potentially other array methods), and then go back using `Object.fromEntries`.
116
+
117
+
Let's do it for our object:
118
+
119
+
```js run
120
+
let prices = {
121
+
banana:1,
122
+
orange:2,
123
+
meat:4,
124
+
};
125
+
126
+
*!*
127
+
let doublePrices =Object.fromEntries(
128
+
// convert to array, map, and then fromEntries gives back the object
129
+
Object.entries(prices).map(([key, value]) => [key, value *2])
130
+
);
131
+
*/!*
132
+
133
+
alert(doublePrices.meat); // 8
134
+
```
135
+
136
+
It may look difficult from the first sight, but becomes easy to understand after you use it once or twice.
137
+
138
+
We also can use `fromEntries` to get an object from `Map`.
139
+
140
+
E.g. we have a `Map` of prices, but we need to pass it to a 3rd-party code that expects an object.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/01-recursion/01-sum-to/solution.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
@@ -37,4 +37,4 @@ P.S. Naturally, the formula is the fastest solution. It uses only 3 operations f
37
37
38
38
The loop variant is the second in terms of speed. In both the recursive and the loop variant we sum the same numbers. But the recursion involves nested calls and execution stack management. That also takes resources, so it's slower.
39
39
40
-
P.P.S. The standard describes a "tail call" optimization: if the recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution and we don't need to remember its execution context. In that case `sumTo(100000)`is countable. But if your JavaScript engine does not support it, there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
40
+
P.P.S. Some engines support the "tail call" optimization: if a recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution, so the engine doesn't need to remember its execution context. That removes the burden on memory, so counting `sumTo(100000)`becomes possible. But if the JavaScript engine does not support tail call optimization (most of them don't), there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/01-recursion/05-output-single-linked-list-reverse/solution.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
@@ -37,7 +37,7 @@ The loop variant is also a little bit more complicated then the direct output.
37
37
38
38
There is no way to get the last value in our `list`. We also can't "go back".
39
39
40
-
So what we can do is to first go through the items in the direct order and rememeber them in an array, and then output what we remembered in the reverse order:
40
+
So what we can do is to first go through the items in the direct order and remember them in an array, and then output what we remembered in the reverse order:
0 commit comments