Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dfb4f00

Browse files
committed
Merge branch 'master' of https://github.com/javascript-tutorial/en.javascript.info into minor-fixes
2 parents 3e2e8c7 + 8be8d6c commit dfb4f00

File tree

31 files changed

+1209
-49
lines changed

31 files changed

+1209
-49
lines changed

‎1-js/05-data-types/03-string/2-check-spam/task.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ importance: 5
44

55
# Check for spam
66

7-
Write a function `checkSpam(str)` that returns `true` if `str` contains 'viagra' or 'XXX', otherwise false.
7+
Write a function `checkSpam(str)` that returns `true` if `str` contains 'viagra' or 'XXX', otherwise `false`.
88

99
The function must be case-insensitive:
1010

‎1-js/05-data-types/04-array/2-create-array/task.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The array in the process:
1616

1717
```js no-beautify
1818
Jazz, Blues
19-
Jazz, Bues, Rock-n-Roll
19+
Jazz, Blues, Rock-n-Roll
2020
Jazz, Classics, Rock-n-Roll
2121
Classics, Rock-n-Roll
2222
Rap, Reggae, Classics, Rock-n-Roll

‎1-js/05-data-types/04-array/article.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ We can use an array as a deque with the following operations:
453453
- `push(...items)` adds `items` to the end.
454454
- `pop()` removes the element from the end and returns it.
455455
- `shift()` removes the element from the beginning and returns it.
456-
- `unshift(...items)` adds items to the beginning.
456+
- `unshift(...items)` adds `items` to the beginning.
457457

458458
To loop over the elements of the array:
459459
- `for (let i=0; i<arr.length; i++)` -- works fastest, old-browser-compatible.

‎1-js/05-data-types/07-map-set-weakmap-weakset/article.md‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,24 @@ alert( visitsCountMap.get(john) ); // 123
5858

5959
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.
6060

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
69+
70+
*!*
71+
// That's what got written!
72+
alert( visitsCountObj["[object Object]"] ); // 123
73+
*/!*
74+
```
75+
76+
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:
6279

6380
```js run
6481
// we add the id field
@@ -159,7 +176,7 @@ The iteration goes in the same order as the values were inserted. `Map` preserve
159176
Besides that, `Map` has a built-in `forEach` method, similar to `Array`:
160177
161178
```js
162-
// runs the function for each (key, value) pair
179+
// runs the function for each (key, value) pair
163180
recipeMap.forEach( (value, key, map) => {
164181
alert(`${key}: ${value}`); // cucumber: 500 etc
165182
});
@@ -172,7 +189,7 @@ A `Set` is a collection of values, where each value may occur only once.
172189
173190
Its main methods are:
174191
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.
176193
- `set.add(value)` -- adds a value, returns the set itself.
177194
- `set.delete(value)` -- removes the value, returns `true` if `value` existed at the moment of the call, otherwise `false`.
178195
- `set.has(value)` -- returns `true` if the value exists in the set, otherwise `false`.

‎1-js/05-data-types/08-keys-values-entries/article.md‎

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
# Object.keys, values, entries
33

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.
55

66
In the previous chapter we saw methods `map.keys()`, `map.values()`, `map.entries()`.
77

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.
99

1010
They are supported for:
1111

@@ -63,8 +63,93 @@ for (let value of Object.values(user)) {
6363
}
6464
```
6565

66-
## Object.keys/values/entries ignore symbolic properties
67-
66+
```warn header="Object.keys/values/entries ignore symbolic properties"
6867
Just like a `for..in` loop, these methods ignore properties that use `Symbol(...)` as keys.
6968
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.
97+
98+
So we can use a loop:
99+
100+
```js run
101+
let prices = {
102+
banana: 1,
103+
orange: 2,
104+
meat: 4,
105+
};
106+
107+
let doublePrices = {};
108+
for(let [product, price] of Object.entries(prices)) {
109+
doublePrices[product] = price * 2;
110+
}
111+
112+
alert(doublePrices.meat); // 8
113+
```
114+
115+
...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.
141+
142+
Here we go:
143+
144+
```js run
145+
let map = new Map();
146+
map.set('banana', 1);
147+
map.set('orange', 2);
148+
map.set('meat', 4);
149+
150+
let obj = Object.fromEntries(map);
151+
152+
// now obj = { banana: 1, orange: 2, meat: 4 }
153+
154+
alert(obj.orange); // 2
155+
```

‎1-js/06-advanced-functions/01-recursion/05-output-single-linked-list-reverse/solution.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The loop variant is also a little bit more complicated then the direct output.
3737

3838
There is no way to get the last value in our `list`. We also can't "go back".
3939

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:
4141

4242
```js run
4343
let list = {
-981 Bytes
Loading[フレーム]
22.5 KB
Loading[フレーム]

‎1-js/12-generators-iterators/1-generators/article.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ If we don't catch the error there, then, as usual, it falls through to the outer
458458

459459
## Summary
460460

461-
- Generators are created by generator functions `function*(...) {...}`.
461+
- Generators are created by generator functions `function* f(...) {...}`.
462462
- Inside generators (only) there exists a `yield` operator.
463463
- The outer code and the generator may exchange results via `next/yield` calls.
464464

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
```js run
3+
let user = {
4+
name: "John"
5+
};
6+
7+
function wrap(target) {
8+
return new Proxy(target, {
9+
get(target, prop, receiver) {
10+
if (prop in target) {
11+
return Reflect.get(target, prop, receiver);
12+
} else {
13+
throw new ReferenceError(`Property doesn't exist: "${prop}"`)
14+
}
15+
}
16+
});
17+
}
18+
19+
user = wrap(user);
20+
21+
alert(user.name); // John
22+
alert(user.age); // Error: Property doesn't exist
23+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /