Skip to content

Commit 185efef

Browse files
committed
ok
1 parent 438e7ae commit 185efef

File tree

9 files changed

+163
-33
lines changed

9 files changed

+163
-33
lines changed

1-js/3-object-basics/2-garbage-collection/article.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ Regularly the following "garbage collection" steps are performed:
151151

152152
- The garbage collector takes roots and "marks" them.
153153
- Then it visits and "marks" all references from them.
154-
- Then it visits marked objects and marks their references (the same object is not visited twice).
155-
- ...And so on until there are unvisited references.
154+
- Then it visits marked objects and marks *their* references, but the same object is not visited twice.
155+
- ...And so on until there are unvisited references (reachable from the roots).
156156
- All objects except marked ones are removed.
157157

158-
For instance, if our object structure might look like this:
158+
For instance, if our object structure looks like this:
159159

160160
![](garbage-collection-1.png)
161161

@@ -182,11 +182,10 @@ Javascript engines apply many optimizations to it, to make it run faster and be
182182
Some of the optimizations:
183183

184184
- **Generational collection** -- objects are split into two sets: "new ones" and "old ones". Many objects appear, do their job and die fast, so they can be cleaned up more aggressively. Those "new" that survive for long enough, become "old".
185-
- **Incremental collection** -- there may be many objects, if we try to clean up the whole object tree at once, it may take some time and introduce visible delays. So the engine tries to split the job into pieces. Then pieces are executed one at a time. That requires some extra bookkeeping in-between to stay consistent.
185+
- **Incremental collection** -- if there are many objects, and we try to walk and mark the whole object set at once, it may take some time and introduce visible delays. So the engine tries to split the job into pieces. Then pieces are executed one at a time. That requires some extra bookkeeping in-between them to stay consistent.
186186
- **Idle-time collection** -- the garbage collector tries to run only while the CPU is idle, to reduce the possible effect on the execution.
187187

188-
In-depth understanding of these optimization is also achievable, there's no magic, but it requires a lot of under-the-hood digging. Javascript engines implement them differently. And -- what's even more important, things change, so going that deep is recommended when you already know the language well and really need low-level optimizations for your code.
189-
188+
In-depth understanding of these optimization is also possible, there's no magic, but it requires a lot of under-the-hood digging. Javascript engines implement garbage collection differently. And -- what's even more important, things change, so going really deep "in advance" is probably not worth that. Unless, of course, it is a matter of pure interest.
190189

191190
## Summary
192191

@@ -197,8 +196,8 @@ The main things to know:
197196

198197
Modern engines implement advanced algorithms of garbage collection.
199198

200-
If you are familiar with low-level programming, the more detailed information about V8 garbage collector is in the article [A tour of V8: Garbage Collection](http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection). Also you'd better prepare yourself by learning how values are stored in V8. I'm saying: "V8", because it is best covered with articles in the internet. For other engines, things are partially similar, but not the same.
199+
If you are familiar with low-level programming, the more detailed information about V8 garbage collector is in the article [A tour of V8: Garbage Collection](http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection). Also you'd better prepare yourself by learning how values are stored in V8. I'm saying: "V8", because it is best covered with articles in the internet. For other engines, things are somewhat similar, but not the same.
201200

202-
In-depth knowledge of engines is good when you need low-level optimizations. It would be wise to plan that as the next step after you're familiar with the language. And when you feel that you need that of course.
201+
In-depth knowledge of engines is good when you need low-level optimizations. It would be wise to plan that as the next step after you're familiar with the language.
203202

204203

1-js/3-object-basics/6-object-toprimitive/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ alert(+user); // 30
136136
alert(user + 1); // 31 (default like number calls valueOf)
137137
```
138138

139-
If we only want a nice debugging output of our object, then we can implement `toString` only. It the absence of `valueOf` it will be used for both conversions.
139+
If we want a single "catch-all" place for primitive conversions, or only want a nice debugging output of our object, then we can implement `toString` only. It the absence of `valueOf` it will be used for all conversions.
140140

141141
For instance:
142142

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
# Function object
3+
4+
Functions in Javascript are two-faced. From one side they are callable "actions". From the other side, they are objects.
5+
6+
We can add/remove properties to them, and they have some useful properties of their own.
7+
8+
## Properties "name" and "length"
9+
10+
For Function Declarations, it's obvious:
11+
12+
```js run
13+
function sayHi() {
14+
alert("Hi");
15+
}
16+
17+
alert(sayHi.name); // sayHi
18+
```
19+
20+
But what's more funny, the "name"-assigning logic is smart. It also recognizes functions, assigned to variables or provided as object methods:
21+
22+
```js run
23+
let sayHi = function() {
24+
alert("Hi");
25+
}
26+
27+
alert(sayHi.name); // sayHi (works!)
28+
```
29+
30+
```js run
31+
let user = {
32+
33+
sayHi() { // works
34+
// ...
35+
},
36+
37+
sayBye: function() { // works too
38+
// ...
39+
}
40+
41+
}
42+
43+
alert(user.sayHi.name); // sayHi
44+
alert(user.sayBye.name); // sayBye
45+
```
46+
47+
In other cases, the name is empty, like here:
48+
49+
```js
50+
let arr = [function() {}];
51+
52+
alert( arr[0].name ); // ""
53+
```
54+
55+
In practice, most functions do have a name. It's mostly used for debugging, or, sometimes,
56+
57+
## Builtin "name" and "length"
58+
59+
Function objects have
60+
61+
We already know that functions in Javascript are objects
62+
Every value in Javascript has the type. What type of value is a function?
63+
64+
In Javascript, a function is an object.
65+
66+
NOT ALL !!! (some inherit?)
67+
68+
For example, all functions have property `name` (function name) and `length` (number of arguments):
69+
70+
```js run
71+
function sayHi() {
72+
alert("Hi");
73+
}
74+
75+
alert( sayHi.name ); // sayHi
76+
alert( sayHi.length ); // 0
77+
```
78+
79+
80+
81+
## Custom properties
82+
83+
Using a function as an object is nothing special.
84+
85+
Here we add the `counter` property to track the total calls count:
86+
87+
```js run
88+
function sayHi() {
89+
alert("Hi");
90+
91+
*!*
92+
// let's count how many times we run
93+
sayHi.counter++;
94+
*/!*
95+
}
96+
sayHi.counter = 0; // initial value
97+
98+
sayHi(); // Hi
99+
sayHi(); // Hi
100+
101+
alert( `Called ${sayHi.counter} times` ); // Called 2 times
102+
```
103+
104+
105+
```warn header="A property is not a variable"
106+
A property assigned to a function like `sayHi.counter = 0` does *not* define a local variable `counter` inside it. In other words, a property `sayHi.counter` and `let counter` inside the function (if we have it) are two unrelated things.
107+
108+
We can treat a function as an object for convenience, store properties in it, that has no effect on its execution.
109+
```
110+
111+
There are many well-known Javascript libraries that make a great use of custom function properties.
112+
113+
They create a "main" function and attach many other "helper" functions to it. For instance, the [jquery](https://jquery.com) library creates a function named `$`. The [lodash](https://lodash.com) library creates a function `_`. And then adds `_.clone`, `_.keyBy` and other properties to (see the [docs](https://lodash.com/docs) when you want learn more about them).
114+
115+
So, a function can do a useful job by itself and also carry a bunch of other functionality in properties.

1-js/4-more-syntax/1-function-arguments-rest-spread/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ alert( Math.max(0, ...arr, 2, ...arr2) ); // 15
155155
When you see `"..."`, there's an easy way to differ spread operator from rest parameters:
156156
157157
- Rest parameters, if it's in the function definition (gathers into array).
158-
- Spread operator, if anywhere else (expands an array).
158+
- Spread operator, if in the function call (expands an array).
159159
```
160160

161161
Also we can use spread operator to merge arrays:
@@ -195,8 +195,8 @@ alert( Array.from(str) ); // H,e,l,l,o
195195

196196
## Summary
197197

198-
- When `...` occurs in function parameters, it's "rest parameters" and gathers the rest of the list into the array.
199-
- When `...` occurs anywhere else, it's called a "spread operator" and expands an array into the list.
198+
- When `...` is at the end of function parameters, it's "rest parameters" and gathers the rest of the list into the array.
199+
- When `...` occurs in a function call, it's called a "spread operator" and expands an array into the list.
200200

201201
Together they help to travel between a list and an array of parameters with ease.
202202

1-js/4-more-syntax/2-destructuring-assignment/article.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,20 @@ alert( title ); // Imperator
4040
In the code above, the first and second elements of the array are skipped, the third one is assigned to `title`, and the rest is also skipped.
4141
````
4242

43-
### The rest operator
43+
### The rest '...'
4444

45-
If we want not just to get first values, but also to gather all that follows -- we can add one more parameter that gets "the rest" using the rest operator `"..."` (three dots):
45+
If we want not just to get first values, but also to gather all that follows -- we can add one more parameter that gets "the rest" using the three dots `"..."`:
4646

4747
```js run
48-
*!*
49-
let [name1, name2, ...rest] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
50-
*/!*
48+
let [name1, name2, *!*...rest*/!*] = ["Julius", "Caesar", *!*"Consul", "of the Roman Republic"*/!*];
5149
5250
alert(name1); // Julius
5351
alert(name2); // Caesar
5452

5553
*!*
5654
alert(rest[0]); // Consul
5755
alert(rest[1]); // of the Roman Republic
56+
alert(rest.length); // 2
5857
*/!*
5958
```
6059

@@ -77,14 +76,25 @@ If we want a "default" value to take place of the absent one, we can provide it
7776
```js run
7877
*!*
7978
// default values
80-
let [name="Guest", surname="Anonymous"] = [];
79+
let [name="Guest", surname="Anonymous"] = ["Julius"];
8180
*/!*
8281

83-
alert(name); // Guest
84-
alert(surname); // Anonymous
82+
alert(name); // Julius (from array)
83+
alert(surname); // Anonymous (default used)
84+
```
85+
86+
Default values can be more complex expressions or even function calls. They are evaluated only if the value is not provided.
87+
88+
For instance, here we use `prompt` function for defaults. But it will run only for the second one, as it's not provided:
89+
90+
```js run
91+
let [name=prompt('name?'), surname=prompt('surname?')] = ["Julius"];
92+
93+
alert(name); // Julius (from array)
94+
alert(surname); // whatever prompt gets
8595
```
8696

87-
Note that default values can be more complex expressions. They are evaluated only if the value is not provided.
97+
8898

8999
## Object destructuring
90100

@@ -185,7 +195,7 @@ What if the object has more properties than we have variables? Can we assign the
185195
Unfortunately, the current specification does not support that feature. There is a proposal, but it's not in the standard yet.
186196

187197
````smart header="Destructuring without `let`"
188-
In the examples above variables were declared right before the assignment: `let {…} = {…}`. Of course, we could use the existing variables too. But there's a catch.
198+
In the examples above variables were declared right before the assignment: `let {…} = {…}`. Of course, we could use existing variables as well. But there's a catch.
189199

190200
This won't work:
191201
```js run
@@ -206,7 +216,7 @@ The problem is that Javascript treats `{...}` in the main code flow (not inside
206216
}
207217
```
208218

209-
To show Javascript that it's not a code block, but a something else, we can wrap the whole assignment in brackets `(...)`:
219+
Of course, we want destructuring assignment, but by syntax rules of Javascript blocks are checked first. To show Javascript that it's not a code block, we can wrap the whole assignment in brackets `(...)`:
210220

211221

212222
```js run
@@ -218,6 +228,7 @@ let title, width, height;
218228

219229
alert( title ); // Menu
220230
```
231+
Brackets do not affect the result, but now the `{...}` thing is not in the main code flow, so code block syntax does not apply, and the engine finally understands it right.
221232
````
222233
223234
## Nested destructuring
@@ -232,7 +243,8 @@ let options = {
232243
width: 100,
233244
height: 200
234245
},
235-
items: ["Cake", "Donut"]
246+
items: ["Cake", "Donut"],
247+
extra: true // something extra that we will not destruct
236248
}
237249

238250
// destructuring assignment on multiple lines for clarity
@@ -242,7 +254,7 @@ let {
242254
height
243255
},
244256
items: [item1, item2], // assign items here
245-
title = "Menu" // an extra property (default value is used)
257+
title = "Menu" // not present in the object (default value is used)
246258
} = options;
247259

248260
alert(title); // Menu
@@ -252,11 +264,13 @@ alert(item1); // Cake
252264
alert(item2); // Donut
253265
```
254266
255-
As we can see, the whole `options` object is correctly assigned to variables.
267+
The whole `options` object except `extra` that was not mentioned, is assigned to corresponding variables.
256268
257-
The left part of the destructuring assignment can combine and nest things as needed.
269+
![](destructuring-complex.png)
258270
259-
## Destructuring function parameters
271+
As we can see, the left part of the destructuring assignment can combine and nest things as needed.
272+
273+
## Smart function parameters
260274
261275
There are times when a function may have many parameters, most of which are optional. That's especially true for user interfaces. Imagine a function that creates a menu. It may have a width, a height, a title, items list and so on.
262276
@@ -283,14 +297,16 @@ Destructuring comes to the rescue!
283297
We can pass parameters as an object, and the function immediately destructurizes them into variables:
284298
285299
```js run
300+
// we pass object to function
286301
let options = {
287302
title: "My menu",
288303
items: ["Item1", "Item2"]
289304
};
290305

291-
*!*
292-
function showMenu({title = "Untitled", width = 200, height = 100, items = []}) {
293-
*/!*
306+
// ...and it immediately expands it to variables
307+
function showMenu(*!*{title = "Untitled", width = 200, height = 100, items = []}*/!*) {
308+
// title, items – taken from options,
309+
// width, height – defaults used
294310
alert( title + ' ' + width + ' ' + height ); // My Menu 100 200
295311
alert( items ); // Item1, Item2
296312
}
@@ -339,7 +355,7 @@ showMenu({});
339355
showMenu();
340356
```
341357

342-
We can fix this by making `{}` a value by default for the whole destructuring thing:
358+
We can fix this by making `{}` the default value for the whole destructuring thing:
343359

344360

345361
```js run
15.6 KB
Loading
38.4 KB
Loading

1-js/plan2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ recursion (
6565
task: traverse list back
6666
)
6767

68-
<< function is object
68+
<< function name
6969
name property (obj props, methods, assignments - set it)
7070
length property
7171

figures.sketch

24 KB
Binary file not shown.

0 commit comments

Comments
 (0)