Skip to content

Commit 28d3b85

Browse files
committed
up
1 parent e002be4 commit 28d3b85

File tree

33 files changed

+1049
-75
lines changed

33 files changed

+1049
-75
lines changed

1-js/2-first-steps/21-object-tostring-valueof/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The `ToPrimitive(obj, "number")` is the same, but `valueOf()` and `toString()` a
4040
4. If the result is a primitive, return it.
4141
5. Otherwise `TypeError` (conversion failed)
4242

43-
```smart header="ToPrimitive returns a primitive"
43+
```smart header="ToPrimitive returns a primitive, but its type is not guaranteed"
4444
As we can see, the result of `ToPrimitive` is always a primitive, because even if `toString/valueOf` return a non-primitive value, it is ignored.
4545
4646
But it can be any primitive. There's no control whether `toString()` returns exactly a string or, say a boolean.
@@ -96,7 +96,7 @@ If only `toString()` is implemented, then both string and numeric conversions us
9696

9797
## Array example
9898

99-
Let's check a few examples to finally get the whole picture.
99+
Let's see few more examples with arrays to get the better picture.
100100

101101
```js run
102102
alert( [] + 1 ); // '1'
@@ -108,7 +108,7 @@ The array from the left side of `+` is first converted to primitive using `toPri
108108

109109
For arrays (and most other built-in objects) only `toString` is implemented, and it returns a list of items.
110110

111-
So we'll have:
111+
So we'll have the following results of conversion:
112112

113113
```js
114114
alert( '' + 1 ); // '1'

1-js/5-deeper/1-recursion/9-output-single-linked-list/task.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ importance: 5
55
# Вывести односвязный список
66

77
TODO: определение списка есть в статье, убрать отсюда.
8+
TODO: разбить на две задачи - прямой и обратный вывод.
89

910
[Односвязный список](http://ru.wikipedia.org/wiki/Связный_список) -- это структура данных, которая состоит из *элементов*, каждый из которых хранит ссылку на следующий. Последний элемент может не иметь ссылки, либо она равна `null`.
1011

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The answer: **0,1.**
2+
3+
Functions `counter` and `counter2` are created by different invocations of `makeCounter`.
4+
5+
So they have independent outer Lexical Environments, each one has it's own `count`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
importance: 5
2+
3+
---
4+
5+
# Are counters independent?
6+
7+
What is the second counter going to show? `0,1` or `2,3` or something else?
8+
9+
```js
10+
function makeCounter() {
11+
let count = 0;
12+
13+
return function() {
14+
return count++;
15+
};
16+
}
17+
18+
var counter = makeCounter();
19+
var counter2 = makeCounter();
20+
21+
alert( counter() ); // 0
22+
alert( counter() ); // 1
23+
24+
*!*
25+
alert( counter2() ); // ?
26+
alert( counter2() ); // ?
27+
*/!*
28+
```
29+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function makeCounter() {
2+
let count = 0;
3+
4+
function counter() {
5+
return count++;
6+
}
7+
8+
counter.set = value => count = value;
9+
10+
counter.decrease = () => count--;
11+
12+
return counter;
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function makeCounter() {
2+
let count = 0;
3+
4+
// ... your code ...
5+
}
6+
7+
let counter = makeCounter();
8+
9+
alert( counter() ); // 0
10+
alert( counter() ); // 1
11+
12+
counter.set(10); // set the new count
13+
14+
alert( counter() ); // 10
15+
16+
counter.decrease(); // decrease the count by 1
17+
18+
alert( counter() ); // 10 (instead of 11)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
describe("counter", function() {
2+
3+
it("increases from call to call", function() {
4+
5+
let counter = makeCounter();
6+
7+
assert.equal( counter(), 0 );
8+
assert.equal( counter(), 1 );
9+
assert.equal( counter(), 2 );
10+
});
11+
12+
13+
describe("counter.set", function() {
14+
it("sets the count", function() {
15+
16+
let counter = makeCounter();
17+
18+
counter.set(10);
19+
20+
assert.equal( counter(), 10 );
21+
assert.equal( counter(), 11 );
22+
});
23+
});
24+
25+
describe("counter.decrease", function() {
26+
it("decreases the count", function() {
27+
28+
let counter = makeCounter();
29+
30+
counter.set(10);
31+
32+
assert.equal( counter(), 10 );
33+
34+
counter.decrease();
35+
36+
assert.equal( counter(), 10 );
37+
38+
});
39+
});
40+
41+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
The solution is to write addition methods right into the `counter`. They share the same outer lexical environment and also can access the current `count`.
3+
4+
This trick is often used for Javascript libraries like lodash, jQuery and others. They provide a function that has other functions as properties.
5+
6+
Actually, they do it to less pollute the global space, so that a single library gives only one global variable. That lowers the chance of possible naming conflicts.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
importance: 5
2+
3+
---
4+
5+
# Set and decrease for counter
6+
7+
Modify the code of `makeCounter()` so that the counter can also decrease and set the number:
8+
9+
- `counter()` should return the next number (as before).
10+
- `counter.set(value)` should set the `count` to `value`.
11+
- `counter.decrease(value)` should decrease the `count` by 1.
12+
13+
See the sandbox code for the complete usage example.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The result is **an error**.
2+
3+
The function `sayHi` is declared inside the `if`, so it only lives inside it. There is no `sayHi` outside.

0 commit comments

Comments
 (0)