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/05-data-types/11-date/article.md
+9-10
Original file line number
Diff line number
Diff line change
@@ -29,13 +29,12 @@ To create a new `Date` object call `new Date()` with one of the following argume
29
29
alert( Jan02_1970 );
30
30
```
31
31
32
-
The number of milliseconds that has passed since the beginning of 1970 is called a *timestamp*.
32
+
An integer number representing the number of milliseconds that has passed since the beginning of 1970 is called a *timestamp*.
33
33
34
34
It's a lightweight numeric representation of a date. We can always create a date from a timestamp using `new Date(timestamp)` and convert the existing `Date` object to a timestamp using the `date.getTime()` method (see below).
35
35
36
36
`new Date(datestring)`
37
-
: If there is a single argument, and it's a string, then it is parsed with the `Date.parse` algorithm (see below).
38
-
37
+
: If there is a single argument, and it's a string, then it is parsed automatically. The algorithm is the same as `Date.parse` uses, we'll cover it later.
39
38
40
39
```js run
41
40
let date = new Date("2017-01-26");
@@ -131,12 +130,12 @@ Besides the given methods, there are two special ones that do not have a UTC-var
131
130
132
131
The following methods allow to set date/time components:
Copy file name to clipboardExpand all lines: 1-js/05-data-types/12-json/2-serialize-event-circular/task.md
+2-3
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ importance: 5
6
6
7
7
In simple cases of circular references, we can exclude an offending property from serialization by its name.
8
8
9
-
But sometimes there are many backreferences. And names may be used both in circular references and normal properties.
9
+
But sometimes we can't just use the name, as it may be used both in circular references and normal properties. So we can check the property by its value.
10
10
11
11
Write `replacer` function to stringify everything, but remove properties that reference `meetup`:
12
12
@@ -22,7 +22,7 @@ let meetup = {
22
22
};
23
23
24
24
*!*
25
-
// circular references
25
+
// circular references
26
26
room.occupiedBy= meetup;
27
27
meetup.self= meetup;
28
28
*/!*
@@ -39,4 +39,3 @@ alert( JSON.stringify(meetup, function replacer(key, value) {
Copy file name to clipboardExpand all lines: 1-js/05-data-types/12-json/article.md
+12-12
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ let user = {
21
21
alert(user); // {name: "John", age: 30}
22
22
```
23
23
24
-
...But in the process of development, new properties are added, old properties are renamed and removed. Updating such `toString` every time can become a pain. We could try to loop over properties in it, but what if the object is complex and has nested objects in properties? We'd need to implement their conversion as well. And, if we're sending the object over a network, then we also need to supply the code to "read" our object on the receiving side.
24
+
...But in the process of development, new properties are added, old properties are renamed and removed. Updating such `toString` every time can become a pain. We could try to loop over properties in it, but what if the object is complex and has nested objects in properties? We'd need to implement their conversion as well.
25
25
26
26
Luckily, there's no need to write the code to handle all this. The task has been solved already.
27
27
@@ -76,7 +76,7 @@ Please note that a JSON-encoded object has several important differences from th
76
76
77
77
`JSON.stringify` can be applied to primitives as well.
Here we are probably too strict. The property list is applied to the whole object structure. So participants are empty, because `name` is not in the list.
216
+
Here we are probably too strict. The property list is applied to the whole object structure. So the objects in `participants` are empty, because `name` is not in the list.
217
217
218
-
Let's include every property except `room.occupiedBy` that would cause the circular reference:
218
+
Let's include in the list every property except `room.occupiedBy` that would cause the circular reference:
219
219
220
220
```js run
221
221
let room = {
@@ -244,7 +244,7 @@ Now everything except `occupiedBy` is serialized. But the list of properties is
244
244
245
245
Fortunately, we can use a function instead of an array as the `replacer`.
246
246
247
-
The function will be called for every `(key, value)` pair and should return the "replaced" value, which will be used instead of the original one.
247
+
The function will be called for every `(key, value)` pair and should return the "replaced" value, which will be used instead of the original one. Or `undefined` if the value is to be skipped.
248
248
249
249
In our case, we can return `value` "as is" for everything except `occupiedBy`. To ignore `occupiedBy`, the code below returns `undefined`:
alert(`${key}: ${value}`);// to see what replacer gets
265
+
alert(`${key}: ${value}`);
266
266
return (key =='occupiedBy') ?undefined: value;
267
267
}));
268
268
@@ -283,7 +283,7 @@ Please note that `replacer` function gets every key/value pair including nested
283
283
284
284
The first call is special. It is made using a special "wrapper object": `{"": meetup}`. In other words, the first `(key, value)` pair has an empty key, and the value is the target object as a whole. That's why the first line is `":[object Object]"` in the example above.
285
285
286
-
The idea is to provide as much power for `replacer` as possible: it has a chance to analyze and replace/skip the whole object if necessary.
286
+
The idea is to provide as much power for `replacer` as possible: it has a chance to analyze and replace/skip even the whole object if necessary.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/01-recursion/article.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -326,18 +326,18 @@ In other words, a company has departments.
326
326
327
327
Now let's say we want a function to get the sum of all salaries. How can we do that?
328
328
329
-
An iterative approach is not easy, because the structure is not simple. The first idea may be to make a `for` loop over `company` with nested subloop over 1st level departments. But then we need more nested subloops to iterate over the staff in 2nd level departments like `sites`. ...And then another subloop inside those for 3rd level departments that might appear in the future? Should we stop on level 3 or make 4 levels of loops? If we put 3-4 nested subloops in the code to traverse a single object, it becomes rather ugly.
329
+
An iterative approach is not easy, because the structure is not simple. The first idea may be to make a `for` loop over `company` with nested subloop over 1st level departments. But then we need more nested subloops to iterate over the staff in 2nd level departments like `sites`... And then another subloop inside those for 3rd level departments that might appear in the future? If we put 3-4 nested subloops in the code to traverse a single object, it becomes rather ugly.
330
330
331
331
Let's try recursion.
332
332
333
333
As we can see, when our function gets a department to sum, there are two possible cases:
334
334
335
-
1. Either it's a "simple" department with an *array of people* -- then we can sum the salaries in a simple loop.
336
-
2. Or it's *an object with `N` subdepartments* -- then we can make `N` recursive calls to get the sum for each of the subdeps and combine the results.
335
+
1. Either it's a "simple" department with an *array* of people -- then we can sum the salaries in a simple loop.
336
+
2. Or it's *an object* with `N` subdepartments -- then we can make `N` recursive calls to get the sum for each of the subdeps and combine the results.
337
337
338
-
The (1) is the base of recursion, the trivial case.
338
+
The 1st case is the base of recursion, the trivial case, when we get an array.
339
339
340
-
The (2) is the recursive step. A complex task is split into subtasks for smaller departments. They may in turn split again, but sooner or later the split will finish at (1).
340
+
The 2nd case when we gen an object is the recursive step. A complex task is split into subtasks for smaller departments. They may in turn split again, but sooner or later the split will finish at (1).
341
341
342
342
The algorithm is probably even easier to read from the code:
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/02-rest-parameters-spread-operator/article.md
+4-5
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ For instance:
8
8
-`Object.assign(dest, src1, ..., srcN)` -- copies properties from `src1..N` into `dest`.
9
9
- ...and so on.
10
10
11
-
In this chapter we'll learn how to do the same. And, more importantly, how to feel comfortable working with such functions and arrays.
11
+
In this chapter we'll learn how to do the same. And also, how to pass arrays to such functions as parameters.
12
12
13
13
## Rest parameters `...`
14
14
@@ -96,9 +96,7 @@ showName("Julius", "Caesar");
96
96
showName("Ilya");
97
97
```
98
98
99
-
In old times, rest parameters did not exist in the language, and using `arguments` was the only way to get all arguments of the function, no matter their total number.
100
-
101
-
And it still works, we can use it today.
99
+
In old times, rest parameters did not exist in the language, and using `arguments` was the only way to get all arguments of the function. And it still works, we can find it in the old code.
102
100
103
101
But the downside is that although `arguments` is both array-like and iterable, it's not an array. It does not support array methods, so we can't call `arguments.map(...)` for example.
104
102
@@ -119,9 +117,10 @@ function f() {
119
117
120
118
f(1); // 1
121
119
```
122
-
````
123
120
124
121
As we remember, arrow functions don't have their own `this`. Now we know they don't have the special `arguments` object either.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/03-closure/article.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ The Lexical Environment object consists of two parts:
70
70
1. *Environment Record* -- an object that stores all local variables as its properties (and some other information like the value of `this`).
71
71
2. A reference to the *outer lexical environment*, the one associated with the outer code.
72
72
73
-
**So, a "variable" is just a property of the special internal object, `Environment Record`. "To get or change a variable" means "to get or change a property of that object".**
73
+
**A "variable" is just a property of the special internal object, `Environment Record`. "To get or change a variable" means "to get or change a property of that object".**
74
74
75
75
For instance, in this simple code, there is only one Lexical Environment:
76
76
@@ -80,7 +80,7 @@ This is a so-called global Lexical Environment, associated with the whole script
80
80
81
81
On the picture above, the rectangle means Environment Record (variable store) and the arrow means the outer reference. The global Lexical Environment has no outer reference, so it points to `null`.
82
82
83
-
Here's the bigger picture of what happens when a `let` changes:
83
+
And that's how it changes when a variable is defined and assigned:
@@ -119,7 +119,7 @@ Now let's go on and explore what happens when a function accesses an outer varia
119
119
120
120
During the call, `say()` uses the outer variable `phrase`, let's look at the details of what's going on.
121
121
122
-
First, when a function runs, a new function Lexical Environment is created automatically. That's a general rule for all functions. That Lexical Environment is used to store local variables and parameters of the call.
122
+
When a function runs, a new Lexical Environment is created automatically to store local variables and parameters of the call.
123
123
124
124
For instance, for `say("John")`, it looks like this (the execution is at the line, labelled with an arrow):
0 commit comments