Skip to content

Commit d27b611

Browse files
committed
2 parents 7a4c1b0 + 9f13849 commit d27b611

File tree

33 files changed

+233
-207
lines changed

33 files changed

+233
-207
lines changed

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

+1-107
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Function expressions and arrows
1+
# Function expressions
22

33
In JavaScript, a function is not a "magical language structure", but a special kind of value.
44

@@ -355,107 +355,6 @@ That's also better for readability, as it's easier to look up `function f(…) {
355355
...But if a Function Declaration does not suit us for some reason, or we need a conditional declaration (we've just seen an example), then Function Expression should be used.
356356
```
357357

358-
359-
## Arrow functions [#arrow-functions]
360-
361-
There's one more very simple and concise syntax for creating functions, that's often better than Function Expressions. It's called "arrow functions", because it looks like this:
362-
363-
364-
```js
365-
let func = (arg1, arg2, ...argN) => expression
366-
```
367-
368-
...This creates a function `func` that has arguments `arg1..argN`, evaluates the `expression` on the right side with their use and returns its result.
369-
370-
In other words, it's roughly the same as:
371-
372-
```js
373-
let func = function(arg1, arg2, ...argN) {
374-
return expression;
375-
};
376-
```
377-
378-
...But much more concise.
379-
380-
Let's see an example:
381-
382-
```js run
383-
let sum = (a, b) => a + b;
384-
385-
/* The arrow function is a shorter form of:
386-
387-
let sum = function(a, b) {
388-
return a + b;
389-
};
390-
*/
391-
392-
alert( sum(1, 2) ); // 3
393-
394-
```
395-
396-
If we have only one argument, then parentheses around parameters can be omitted, making that even shorter:
397-
398-
```js run
399-
// same as
400-
// let double = function(n) { return n * 2 }
401-
*!*
402-
let double = n => n * 2;
403-
*/!*
404-
405-
alert( double(3) ); // 6
406-
```
407-
408-
If there are no arguments, parentheses should be empty (but they should be present):
409-
410-
```js run
411-
let sayHi = () => alert("Hello!");
412-
413-
sayHi();
414-
```
415-
416-
Arrow functions can be used in the same way as Function Expressions.
417-
418-
For instance, here's the rewritten example with `welcome()`:
419-
420-
```js run
421-
let age = prompt("What is your age?", 18);
422-
423-
let welcome = (age < 18) ?
424-
() => alert('Hello') :
425-
() => alert("Greetings!");
426-
427-
welcome(); // ok now
428-
```
429-
430-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
431-
432-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
433-
434-
```smart header="Multiline arrow functions"
435-
436-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
437-
438-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
439-
440-
Like this:
441-
442-
```js run
443-
let sum = (a, b) => { // the curly brace opens a multiline function
444-
let result = a + b;
445-
*!*
446-
return result; // if we use curly braces, use return to get results
447-
*/!*
448-
};
449-
450-
alert( sum(1, 2) ); // 3
451-
```
452-
453-
```smart header="More to come"
454-
Here we praised arrow functions for brevity. But that's not all! Arrow functions have other interesting features. We'll return to them later in the chapter <info:arrow-functions>.
455-
456-
For now, we can already use arrow functions for one-line actions and callbacks.
457-
```
458-
459358
## Summary
460359

461360
- Functions are values. They can be assigned, copied or declared in any place of the code.
@@ -467,8 +366,3 @@ For now, we can already use arrow functions for one-line actions and callbacks.
467366
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
468367

469368
So we should use a Function Expression only when a Function Declaration is not fit for the task. We've seen a couple of examples of that in this chapter, and will see more in the future.
470-
471-
Arrow functions are handy for one-liners. They come in two flavors:
472-
473-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
474-
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.

1-js/02-first-steps/15-function-expressions-arrows/1-rewrite-arrow/task.md 1-js/02-first-steps/16-arrow-functions-basics/1-rewrite-arrow/task.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Rewrite with arrow functions
33

4-
Replace Function Expressions with arrow functions in the code:
4+
Replace Function Expressions with arrow functions in the code below:
55

66
```js run
77
function ask(question, yes, no) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Arrow functions, the basics
2+
3+
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
4+
5+
It's called "arrow functions", because it looks like this:
6+
7+
```js
8+
let func = (arg1, arg2, ...argN) => expression
9+
```
10+
11+
...This creates a function `func` that accepts arguments `arg1..argN`, then evaluates the `expression` on the right side with their use and returns its result.
12+
13+
In other words, it's the shorter version of:
14+
15+
```js
16+
let func = function(arg1, arg2, ...argN) {
17+
return expression;
18+
};
19+
```
20+
21+
Let's see a concrete example:
22+
23+
```js run
24+
let sum = (a, b) => a + b;
25+
26+
/* This arrow function is a shorter form of:
27+
28+
let sum = function(a, b) {
29+
return a + b;
30+
};
31+
*/
32+
33+
alert( sum(1, 2) ); // 3
34+
```
35+
36+
As you can, see `(a, b) => a + b` means a function that accepts two arguments named `a` and `b`. Upon the execution, it evaluates the expression `a + b` and returns the result.
37+
38+
- If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
39+
40+
For example:
41+
42+
```js run
43+
*!*
44+
let double = n => n * 2;
45+
// roughly the same as: let double = function(n) { return n * 2 }
46+
*/!*
47+
48+
alert( double(3) ); // 6
49+
```
50+
51+
- If there are no arguments, parentheses will be empty (but they should be present):
52+
53+
```js run
54+
let sayHi = () => alert("Hello!");
55+
56+
sayHi();
57+
```
58+
59+
Arrow functions can be used in the same way as Function Expressions.
60+
61+
For instance, to dynamically create a function:
62+
63+
```js run
64+
let age = prompt("What is your age?", 18);
65+
66+
let welcome = (age < 18) ?
67+
() => alert('Hello') :
68+
() => alert("Greetings!");
69+
70+
welcome(); // ok now
71+
```
72+
73+
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
74+
75+
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
76+
77+
## Multiline arrow functions
78+
79+
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
80+
81+
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
82+
83+
Like this:
84+
85+
```js run
86+
let sum = (a, b) => { // the curly brace opens a multiline function
87+
let result = a + b;
88+
*!*
89+
return result; // if we use curly braces, then we need an explicit "return"
90+
*/!*
91+
};
92+
93+
alert( sum(1, 2) ); // 3
94+
```
95+
96+
```smart header="More to come"
97+
Here we praised arrow functions for brevity. But that's not all!
98+
99+
Arrow functions have other interesting features.
100+
101+
To study them in-depth, we first need to get to know some other aspects of JavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
102+
103+
For now, we can already use arrow functions for one-line actions and callbacks.
104+
```
105+
106+
## Summary
107+
108+
Arrow functions are handy for one-liners. They come in two flavors:
109+
110+
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
111+
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.

1-js/02-first-steps/16-javascript-specials/article.md 1-js/02-first-steps/17-javascript-specials/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ We covered three ways to create a function in JavaScript:
272272
- Parameters can have default values: `function sum(a = 1, b = 2) {...}`.
273273
- Functions always return something. If there's no `return` statement, then the result is `undefined`.
274274
275-
Details: see <info:function-basics>, <info:function-expressions-arrows>.
275+
Details: see <info:function-basics>, <info:arrow-functions-basics>.
276276
277277
## More to come
278278

1-js/04-object-basics/05-object-toprimitive/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ These methods must return a primitive value. If `toString` or `valueOf` returns
123123
By default, a plain object has following `toString` and `valueOf` methods:
124124

125125
- The `toString` method returns a string `"[object Object]"`.
126-
- The `valueOf` method returns an object itself.
126+
- The `valueOf` method returns the object itself.
127127

128128
Here's the demo:
129129

@@ -199,7 +199,7 @@ In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise there wil
199199

200200
## Further conversions
201201

202-
As we know already, many operators and functions perform type conversions, e.g. multiplication `*` converts operatnds to numbers.
202+
As we know already, many operators and functions perform type conversions, e.g. multiplication `*` converts operands to numbers.
203203

204204
If we pass an object as an argument, then there are two stages:
205205
1. The object is converted to a primitive (using the rules described above).

1-js/05-data-types/02-number/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Strange! What is it then if not `0.3`?
201201
alert( 0.1 + 0.2 ); // 0.30000000000000004
202202
```
203203

204-
Ouch! There are more consequences than an incorrect comparison here. Imagine you're making an e-shopping site and the visitor puts `$0.10` and `$0.20` goods into their chart. The order total will be `$0.30000000000000004`. That would surprise anyone.
204+
Ouch! There are more consequences than an incorrect comparison here. Imagine you're making an e-shopping site and the visitor puts `$0.10` and `$0.20` goods into their cart. The order total will be `$0.30000000000000004`. That would surprise anyone.
205205

206206
But why does this happen?
207207

1-js/05-data-types/05-array-methods/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ alert(arr); // *!*1, 2, 15*/!*
447447
````
448448

449449
````smart header="Arrow functions for the best"
450-
Remember [arrow functions](info:function-expressions-arrows#arrow-functions)? We can use them here for neater sorting:
450+
Remember [arrow functions](info:arrow-functions-basics)? We can use them here for neater sorting:
451451
452452
```js
453453
arr.sort( (a, b) => a - b );

1-js/05-data-types/06-iterable/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ let arr = Array.from(arrayLike); // (*)
214214
alert(arr.pop()); // World (method works)
215215
```
216216

217-
`Array.from` at the line `(*)` takes the object, examines it for being an iterable or array-like, then makes a new array and copies there all items.
217+
`Array.from` at the line `(*)` takes the object, examines it for being an iterable or array-like, then makes a new array and copies all items to it.
218218

219219
The same happens for an iterable:
220220

1-js/05-data-types/11-date/article.md

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ To create a new `Date` object call `new Date()` with one of the following argume
3333

3434
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).
3535

36+
Dates before 01.01.1970 have negative timestamps, e.g.:
37+
```js run
38+
// 31 Dec 1969
39+
let Dec31_1969 = new Date(-24 * 3600 * 1000);
40+
alert( Dec31_1969 );
41+
```
42+
3643
`new Date(datestring)`
3744
: 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.
3845

1-js/09-classes/01-class/article.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ alert(User.prototype.sayHi); // alert(this.name);
116116
alert(Object.getOwnPropertyNames(User.prototype)); // constructor, sayHi
117117
```
118118

119-
## Not just a syntax sugar
119+
## Not just a syntactic sugar
120120

121-
Sometimes people say that `class` is a "syntax sugar" (syntax that is designed to make things easier to read, but doesn't introduce anything new), because we could actually declare the same without `class` keyword at all:
121+
Sometimes people say that `class` is a "syntactic sugar" (syntax that is designed to make things easier to read, but doesn't introduce anything new), because we could actually declare the same without `class` keyword at all:
122122

123123
```js run
124124
// rewriting class User in pure functions
@@ -140,7 +140,7 @@ let user = new User("John");
140140
user.sayHi();
141141
```
142142

143-
The result of this definition is about the same. So, there are indeed reasons why `class` can be considered a syntax sugar to define a constructor together with its prototype methods.
143+
The result of this definition is about the same. So, there are indeed reasons why `class` can be considered a syntactic sugar to define a constructor together with its prototype methods.
144144

145145
Still, there are important differences.
146146

@@ -282,7 +282,7 @@ Object.defineProperties(User.prototype, {
282282
});
283283
```
284284

285-
Here's an example with a computed property in brackets `[...]`:
285+
Here's an example with a computed property name in brackets `[...]`:
286286

287287
```js run
288288
class User {
@@ -318,6 +318,9 @@ class User {
318318
}
319319

320320
new User().sayHi();
321+
322+
alert(User.prototype.sayHi); // placed in User.prototype
323+
alert(User.prototype.name); // undefined, not placed in User.prototype
321324
```
322325

323326
The property `name` is not placed into `User.prototype`. Instead, it is created by `new` before calling the constructor, it's a property of the object itself.

1-js/09-classes/02-class-inheritance/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The syntax to extend another class is: `class Child extends Parent`.
4040

4141
Let's create `class Rabbit` that inherits from `Animal`:
4242

43-
```js run
43+
```js
4444
*!*
4545
class Rabbit extends Animal {
4646
*/!*

0 commit comments

Comments
 (0)