Skip to content

Commit bef864a

Browse files
committed
ok
1 parent 185efef commit bef864a

File tree

36 files changed

+1248
-334
lines changed

36 files changed

+1248
-334
lines changed

1-js/2-first-steps/15-function-expressions-arrows/article.md

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ function sayHi() {
1010
}
1111
```
1212

13-
There is another way of creating a function that is called *Function Expression*:
13+
There is another way of creating a function that is called *Function Expression*.
14+
15+
[cut]
16+
17+
It looks like this:
1418

1519
```js
1620
let sayHi = function() {
@@ -91,6 +95,68 @@ The answer is simple:
9195
- The Function Expression appears in the context of the statement: `let sayHi = value;`. It's not a code block. The semicolon `;` is recommended at the end of statements, no matter what is the `value`. So the semicolon here is not related to Function Expression itself in any way, it just terminates the statement.
9296
````
9397

98+
## Anonymous functions
99+
100+
Let's see an example where function expressions come really handy.
101+
102+
We'll write a function `ask(question, yes, no)` with three parameters:
103+
104+
`question`
105+
: Text of the question
106+
107+
`yes`
108+
: Function to run if the answer is "Yes"
109+
110+
`no`
111+
: Function to run if the answer is "No"
112+
113+
The function should ask the `question` and, depending on the user's agreement, call `yes()` or `no()`:
114+
115+
```js run
116+
*!*
117+
function ask(question, yes, no) {
118+
if (confirm(question)) yes()
119+
else no();
120+
}
121+
*/!*
122+
123+
function showOk() {
124+
alert( "You agreed." );
125+
}
126+
127+
function showCancel() {
128+
alert( "You canceled the execution." );
129+
}
130+
131+
// usage
132+
ask("Do you agree?", showOk, showCancel);
133+
```
134+
135+
The code looks kind of too simple, right? Why would anyone need such `ask`?
136+
137+
...It turns out that in the browser such functions are very required, the minor difference is that they ask not with a simple `confirm`, but output a much richer looking question window. But that's another story.
138+
139+
Right now let's see how we can write the same much shorter:
140+
141+
```js run no-beautify
142+
function ask(question, yes, no) {
143+
if (confirm(question)) yes()
144+
else no();
145+
}
146+
147+
*!*
148+
ask(
149+
"Do you agree?",
150+
function() { alert("You agreed."); },
151+
function() { alert("You canceled the execution."); }
152+
);
153+
*/!*
154+
```
155+
156+
Here functions are declared right inside the `ask(...)` call. They have no name (anonymous) and are not accessible outside of `ask`, but that's just what we need.
157+
158+
Such code appears very naturally, it's in the spirit of Javascript.
159+
94160

95161
```smart header="A function is a value representing an \"action\""
96162
Regular values like strings or numbers represent the *data*.
@@ -365,7 +431,7 @@ let sum = (a, b) => { // the figure bracket opens a multiline function
365431
*!*
366432
return result; // if we use figure brackets, must use return
367433
*/!*
368-
}
434+
};
369435
370436
alert( sum(1, 2) ); // 3
371437
```
@@ -376,29 +442,6 @@ Here we praised arrow functions for shortness. But that's not all! Arrow functio
376442
As for now, we can already use them for one-line actions.
377443
```
378444

379-
## new Function
380-
381-
And the last syntax for the functions:
382-
```js
383-
let func = new Function('a, b', 'return a + b');
384-
```
385-
386-
The major difference is that it creates a function literally from a string, at run time. See, both arguments are strings. The first one lists the arguments, while the second one is the function body.
387-
388-
All previous declarations required us, programmers, to write the function code in the script.
389-
390-
But `new Function` allows to turn any string into a function, for example we can receive a new function from the server and then execute it:
391-
392-
```js
393-
let str = ... receive the code from the server dynamically ...
394-
395-
let func = new Function('', str);
396-
func();
397-
```
398-
399-
It is used in very specific cases, like when we receive the code from the server, or to dynamically compile a function from a template. The need for such uses arises at advanced stages of the development.
400-
401-
402445
## Summary
403446

404447
[todo review]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ In-depth understanding of these optimization is also possible, there's no magic,
191191

192192
The main things to know:
193193

194+
- Garbage collection is performed automatically. We cannot force or prevent it.
194195
- Objects are retained in memory while they are reachable.
195196
- Being referenced is not the same as being reachable (from a root): a pack of interlinked objects can become unreachable as a whole.
196197

1-js/3-object-basics/9-function-object/article.md

Lines changed: 0 additions & 115 deletions
This file was deleted.

1-js/7-deeper/2-closure/2-counter-inc-dec/solution.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

1-js/7-deeper/2-closure/8-make-army/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ army[0](); // 0
9898
army[5](); // 5
9999
```
100100
101-
Look at the trick. The `while` loop just like `for`, makes a new Lexical Environment for each run. So we must make sure that it gets the right value for a `shooter` will access it.
101+
Look at the trick. The `while` loop, just like `for`, makes a new Lexical Environment for each run. So we must make sure that it gets the right value for a `shooter` will access it.
102102
103-
We copy `let j = i`. This makes a loop-local `j` and copies the value of `i` to it. Primitives are copied "by value", so we actually get a complete independent copy of `i`, belonging to the current loop iteration.
103+
We copy `let j = i`. This makes a loop body local `j` and copies the value of `i` to it. Primitives are copied "by value", so we actually get a complete independent copy of `i`, belonging to the current loop iteration.
104104

1-js/7-deeper/2-closure/8-make-army/task.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ function makeArmy() {
1414

1515
let i = 0;
1616
while (i < 10) {
17-
let j = i;
1817
let shooter = function() { // shooter function
19-
alert( j ); // should show its number
18+
alert( i ); // should show its number
2019
};
2120
shooters.push(shooter);
2221
i++;

0 commit comments

Comments
 (0)