Skip to content

Commit 1916dba

Browse files
committed
merging all conflicts
2 parents b642f3e + 53b35c1 commit 1916dba

File tree

8 files changed

+94
-28
lines changed

8 files changed

+94
-28
lines changed

1-js/02-first-steps/15-function-basics/article.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,15 @@ function zobrazZprávu(odKoho, text) {
266266
### Alternativní výchozí argumenty
267267
Někdy má smysl nenastavovat výchozí hodnoty argumentů v deklaraci funkce, ale až později.
268268
269+
<<<<<<< HEAD
269270
Abychom během provádění funkce ověřili, zda argument byl předán, můžeme jej porovnat s `undefined`:
271+
=======
272+
### Alternative default parameters
273+
274+
Sometimes it makes sense to assign default values for parameters at a later stage after the function declaration.
275+
276+
We can check if the parameter is passed during the function execution, by comparing it with `undefined`:
277+
>>>>>>> 53b35c16835b7020a0a5046da5a47599d313bbb8
270278
271279
```js run
272280
function zobrazZprávu(text) {

1-js/04-object-basics/02-object-copy/article.md

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ alert( a == b ); // false
100100
101101
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj == 5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons are needed very rarely -- usually they appear as a result of a programming mistake.
102102
103+
````smart header="Const objects can be modified"
104+
An important side effect of storing objects as references is that an object declared as `const` *can* be modified.
105+
106+
For instance:
107+
108+
```js run
109+
const user = {
110+
name: "John"
111+
};
112+
113+
*!*
114+
user.name = "Pete"; // (*)
115+
*/!*
116+
117+
alert(user.name); // Pete
118+
```
119+
120+
It might seem that the line `(*)` would cause an error, but it does not. The value of `user` is constant, it must always reference the same object, but properties of that object are free to change.
121+
122+
In other words, the `const user` gives an error only if we try to set `user=...` as a whole.
123+
124+
That said, if we really need to make constant object properties, it's also possible, but using totally different methods. We'll mention that in the chapter <info:property-descriptors>.
125+
````
126+
103127
## Cloning and merging, Object.assign [#cloning-and-merging-object-assign]
104128

105129
So, copying an object variable creates one more reference to the same object.
@@ -136,7 +160,7 @@ We can also use the method [Object.assign](https://developer.mozilla.org/en-US/d
136160
The syntax is:
137161

138162
```js
139-
Object.assign(dest, [src1, src2, src3...])
163+
Object.assign(dest, src1[, src2, src3...])
140164
```
141165

142166
- The first argument `dest` is a target object.
@@ -219,42 +243,76 @@ let clone = Object.assign({}, user);
219243
alert( user.sizes === clone.sizes ); // true, same object
220244
221245
// user and clone share sizes
222-
user.sizes.width++; // change a property from one place
223-
alert(clone.sizes.width); // 51, get the result from the other one
246+
user.sizes.width = 60; // change a property from one place
247+
alert(clone.sizes.width); // 60, get the result from the other one
224248
```
225249
226-
To fix that and make `user` and `clone` truly separate objects, we should use a cloning loop that examines each value of `user[key]` and, if it's an object, then replicate its structure as well. That is called a "deep cloning".
250+
To fix that and make `user` and `clone` truly separate objects, we should use a cloning loop that examines each value of `user[key]` and, if it's an object, then replicate its structure as well. That is called a "deep cloning" or "structured cloning". There's [structuredClone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) method that implements deep cloning.
227251
228-
We can use recursion to implement it. Or, to not reinvent the wheel, take an existing implementation, for instance [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) from the JavaScript library [lodash](https://lodash.com).
229252
230-
````smart header="Const objects can be modified"
231-
An important side effect of storing objects as references is that an object declared as `const` *can* be modified.
253+
### structuredClone
232254
233-
For instance:
255+
The call `structuredClone(object)` clones the `object` with all nested properties.
256+
257+
Here's how we can use it in our example:
234258

235259
```js run
236-
const user = {
237-
name: "John"
260+
let user = {
261+
name: "John",
262+
sizes: {
263+
height: 182,
264+
width: 50
265+
}
238266
};
239267
240268
*!*
241-
user.name = "Pete"; // (*)
269+
let clone = structuredClone(user);
242270
*/!*
243271
244-
alert(user.name); // Pete
272+
alert( user.sizes === clone.sizes ); // false, different objects
273+
274+
// user and clone are totally unrelated now
275+
user.sizes.width = 60; // change a property from one place
276+
alert(clone.sizes.width); // 50, not related
245277
```
246278

247-
It might seem that the line `(*)` would cause an error, but it does not. The value of `user` is constant, it must always reference the same object, but properties of that object are free to change.
279+
The `structuredClone` method can clone most data types, such as objects, arrays, primitive values.
248280

249-
In other words, the `const user` gives an error only if we try to set `user=...` as a whole.
281+
It also supports circular references, when an object property references the object itself (directly or via a chain or references).
250282

251-
That said, if we really need to make constant object properties, it's also possible, but using totally different methods. We'll mention that in the chapter <info:property-descriptors>.
252-
````
283+
For instance:
284+
285+
```js run
286+
let user = {};
287+
// let's create a circular reference:
288+
// user.me references the user itself
289+
user.me = user;
290+
291+
let clone = structuredClone(user);
292+
alert(clone.me === clone); // true
293+
```
294+
295+
As you can see, `clone.me` references the `clone`, not the `user`! So the circular reference was cloned correctly as well.
296+
297+
Although, there are cases when `structuredClone` fails.
298+
299+
For instance, when an object has a function property:
300+
301+
```js run
302+
// error
303+
structuredClone({
304+
f: function() {}
305+
});
306+
```
307+
308+
Function properties aren't supported.
309+
310+
To handle such complex cases we may need to use a combination of cloning methods, write custom code or, to not reinvent the wheel, take an existing implementation, for instance [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) from the JavaScript library [lodash](https://lodash.com).
253311

254312
## Summary
255313

256314
Objects are assigned and copied by reference. In other words, a variable stores not the "object value", but a "reference" (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.
257315

258316
All operations via copied references (like adding/removing properties) are performed on the same single object.
259317

260-
To make a "real copy" (a clone) we can use `Object.assign` for the so-called "shallow copy" (nested objects are copied by reference) or a "deep cloning" function, such as [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep).
318+
To make a "real copy" (a clone) we can use `Object.assign` for the so-called "shallow copy" (nested objects are copied by reference) or a "deep cloning" function `structuredClone` or use a custom cloning implementation, such as [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep).

1-js/09-classes/07-mixins/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Here's the diagram (see the right part):
103103

104104
That's because methods `sayHi` and `sayBye` were initially created in `sayHiMixin`. So even though they got copied, their `[[HomeObject]]` internal property references `sayHiMixin`, as shown in the picture above.
105105

106-
As `super` looks for parent methods in `[[HomeObject]].[[Prototype]]`, that means it searches `sayHiMixin.[[Prototype]]`, not `User.[[Prototype]]`.
106+
As `super` looks for parent methods in `[[HomeObject]].[[Prototype]]`, that means it searches `sayHiMixin.[[Prototype]]`.
107107

108108
## EventMixin
109109

1-js/11-async/03-promise-chaining/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ This feature allows us to integrate custom objects with promise chains without h
224224

225225
## Bigger example: fetch
226226

227-
In frontend programming promises are often used for network requests. So let's see an extended example of that.
227+
In frontend programming, promises are often used for network requests. So let's see an extended example of that.
228228

229229
We'll use the [fetch](info:fetch) method to load the information about the user from the remote server. It has a lot of optional parameters covered in [separate chapters](info:fetch), but the basic syntax is quite simple:
230230

1-js/13-modules/01-modules-intro/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ To name some (for historical reasons):
1313
- [CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1) -- the module system created for Node.js server.
1414
- [UMD](https://github.com/umdjs/umd) -- one more module system, suggested as a universal one, compatible with AMD and CommonJS.
1515

16-
Now all these slowly become a part of history, but we still can find them in old scripts.
16+
Now these all slowly became a part of history, but we still can find them in old scripts.
1717

1818
The language-level module system appeared in the standard in 2015, gradually evolved since then, and is now supported by all major browsers and in Node.js. So we'll study the modern JavaScript modules from now on.
1919

2-ui/1-document/06-dom-attributes-and-properties/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ In the example below `id` is modified as an attribute, and we can see the proper
162162
</script>
163163
```
164164

165-
But there are exclusions, for instance `input.value` synchronizes only from attribute -> to property, but not back:
165+
But there are exclusions, for instance `input.value` synchronizes only from attribute -> property, but not back:
166166

167167
```html run
168168
<input>

5-network/11-websocket/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Sec-WebSocket-Version: 13
9191
- `Origin` -- the origin of the client page, e.g. `https://javascript.info`. WebSocket objects are cross-origin by nature. There are no special headers or other limitations. Old servers are unable to handle WebSocket anyway, so there are no compatibility issues. But the `Origin` header is important, as it allows the server to decide whether or not to talk WebSocket with this website.
9292
- `Connection: Upgrade` -- signals that the client would like to change the protocol.
9393
- `Upgrade: websocket` -- the requested protocol is "websocket".
94-
- `Sec-WebSocket-Key` -- a random browser-generated key for security.
94+
- `Sec-WebSocket-Key` -- a random browser-generated key, used to ensure that the server supports WebSocket protocol. It's random to prevent proxies from caching any following communication.
9595
- `Sec-WebSocket-Version` -- WebSocket protocol version, 13 is the current one.
9696

9797
```smart header="WebSocket handshake can't be emulated"
@@ -107,7 +107,7 @@ Connection: Upgrade
107107
Sec-WebSocket-Accept: hsBlbuDTkk24srzEOTBUlZAlC2g=
108108
```
109109

110-
Here `Sec-WebSocket-Accept` is `Sec-WebSocket-Key`, recoded using a special algorithm. The browser uses it to make sure that the response corresponds to the request.
110+
Here `Sec-WebSocket-Accept` is `Sec-WebSocket-Key`, recoded using a special algorithm. Upon seeing it, the browser understands that the server really does support the WebSocket protocol.
111111

112112
Afterwards, the data is transferred using the WebSocket protocol, we'll see its structure ("frames") soon. And that's not HTTP at all.
113113

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The Modern JavaScript Tutorial
22

3-
This repository hosts the English content of the Modern JavaScript Tutorial, published in [https://javascript.info](https://javascript.info).
3+
This repository hosts the English content of the Modern JavaScript Tutorial, published at [https://javascript.info](https://javascript.info).
44

55
## Translations
66

@@ -12,17 +12,17 @@ See <https://javascript.info/translate> for the details.
1212

1313
We'd also like to collaborate on the tutorial with other people.
1414

15-
Something's wrong? A topic is missing? Explain it to people, add as PR 👏
15+
Something's wrong? A topic is missing? Explain it to people, add it as PR 👏
1616

17-
**You can edit the text in any editor.** The tutorial uses enhanced "markdown" format, easy to grasp. And if you want to see how it looks on-site, there's a server to run the tutorial locally at <https://github.com/javascript-tutorial/server>.
17+
**You can edit the text in any editor.** The tutorial uses an enhanced "markdown" format, easy to grasp. And if you want to see how it looks on-site, there's a server to run the tutorial locally at <https://github.com/javascript-tutorial/server>.
1818

1919
The list of contributors is available at <https://javascript.info/about#contributors>.
2020

2121
## Structure
2222

23-
Every chapter, article or a task has its folder.
23+
Every chapter, article, or task has its folder.
2424

25-
The folder is named like `N-url`, where `N` is a number for the sorting purposes and `url` is the URL part with title of the material.
25+
The folder is named like `N-url`, where `N` is a number for the sorting purposes and `URL` is the URL part with the title of the material.
2626

2727
The type of the material is defined by the file inside the folder:
2828

0 commit comments

Comments
 (0)