Skip to content

Commit 928cd27

Browse files
committed
translating
1 parent 2b874a7 commit 928cd27

File tree

165 files changed

+1975
-2896
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+1975
-2896
lines changed

1-js/2-first-steps/19-function-expression/article.md

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ We can copy it between variables and run when we want. We can even add propertie
101101
```
102102

103103

104-
105104
## Function Expression as a method
106105

107106
Now let's go back: we have two ways of declaring a function. Do we really need both? What's about Function Expressions that makes it a good addition?
@@ -353,8 +352,97 @@ As a rule of thumb, a Function Declaration is prefered. It gives more freedom in
353352
But if a Function Declaration does not suit us for some reason, then a Function Expression should be used.
354353
```
355354

355+
## Arrow functions basics [#arrow-functions]
356+
357+
Enough with the complexities for now. Let's relax with arrow functions that may add elegance to our code.
358+
359+
They act like a function expression, but look a little bit differently.
360+
361+
The syntax:
362+
363+
```js
364+
let func = (arg1, arg2, ...argN) => expression
365+
```
366+
367+
...Creates a function that gets arguments `arg1..argN`, evaludates the `expression` on the right side with their use and returns its result.
368+
369+
It is roughly the same as:
370+
371+
```js
372+
let func = function(arg1, arg2, ...argN) {
373+
return expression;
374+
}
375+
```
376+
377+
Let's see the example:
378+
379+
```js run
380+
let sum = (a, b) => a + b;
381+
382+
alert( sum(1, 2) ); // 3
383+
```
384+
385+
For a single argument function, we can omit the brackets, making that even shorter:
386+
387+
```js run
388+
// brackets () not needed around a single argument n
389+
let double = n => n*2;
390+
391+
alert( double(3) ); // 6
392+
```
393+
394+
As you can see, this extra-concise syntax is well-suited for one-line functions. Very handy to create a function in the middle of a more complex expresion.
395+
396+
Note that to the right side of `=>` must be a single valid expression.
397+
398+
If we need something more complex, like multiple statements, we can enclose it in figure brackets. Then use a normal `return` within them.
399+
400+
Like here:
401+
402+
```js run
403+
let sum = (a, b) => {
404+
let result = a + b;
405+
*!*
406+
return result; // if we use figure brackets, must use return
407+
*/!*
408+
}
409+
410+
alert( sum(1, 2) ); // 3
411+
```
412+
413+
```smart header="More to come"
414+
Arrow functions have other interesting features in them. We'll get to them later.
415+
416+
But as for now, we can already use them for one-line actions.
417+
```
418+
419+
## new Function
420+
421+
And the last syntax for the functions:
422+
```js
423+
let func = new Function('a, b', 'return a + b');
424+
```
425+
426+
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.
427+
428+
All previous declarations required us, programmers, to write the function code in the script.
429+
430+
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:
431+
432+
```js
433+
let str = ... receive the code from the server dynamically ...
434+
435+
let func = new Function('', str);
436+
func();
437+
```
438+
439+
It is used in very specific cases, like when we receive a code from the server as a string, or to dynamically compile a function from a template string. The need for such uses arises at more advanced stages of the development.
440+
441+
356442
## Summary
357443

444+
445+
358446
- Functions are values. They can be assigned, copied or declared in any place of the code.
359447
- If the function is declared as a separate statement -- it's called a Function Declaration.
360448
- If the function is created as a part of an expression -- it's a Function Expression.
@@ -377,4 +465,14 @@ function f() { ... }
377465

378466
Function Declaration is shorter and more obvious. The additional bonus -- it can be called before the actual declaration.
379467

380-
Use Function Expression to write elegant code when the function must be created at-place, inside another expression or when Function Declaration doesn't fit well for the task.
468+
**Use Function Expression to write elegant code when the function must be created at-place, inside another expression or when Function Declaration doesn't fit well for the task.**
469+
470+
We also touched two other ways to create a function:
471+
472+
- Arrow functions: `(...args) => expr` or `{ ... }`
473+
474+
They provide a short way to create a function that evaluates the given `expr`. More to come about them.
475+
476+
- `new Function(args, body)`
477+
478+
This syntax allows to create a function from a string, that may be composed dynamically during the execution, from the incoming data.

1-js/4-data-structures/10-arguments-pseudoarray/1-check-arguments-undefined/solution.md

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

1-js/4-data-structures/10-arguments-pseudoarray/1-check-arguments-undefined/task.md

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

1-js/4-data-structures/10-arguments-pseudoarray/2-sum-arguments/solution.md

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

1-js/4-data-structures/10-arguments-pseudoarray/2-sum-arguments/task.md

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

0 commit comments

Comments
 (0)