Skip to content

Commit 6a263b1

Browse files
committed
work
1 parent 276065b commit 6a263b1

File tree

35 files changed

+845
-486
lines changed

35 files changed

+845
-486
lines changed

1-js/3-object-basics/8-iterable/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ while(true) {
132132
133133
The same works for an array.
134134
135-
## Iterables VS array-likes
135+
## Iterables VS array-likes [#array-like]
136136
137137
There are two official terms that are similar, but actually very different. Please take care to avoid the confusion.
138138
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function delay(f, ms) {
2+
3+
return function() {
4+
setTimeout(() => f.apply(this, arguments), ms);
5+
};
6+
7+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
describe("delay", function() {
2+
before(function() {
3+
this.clock = sinon.useFakeTimers();
4+
});
5+
6+
after(function() {
7+
this.clock.restore();
8+
});
9+
10+
it("вызывает функцию через указанный таймаут", function() {
11+
var start = Date.now();
12+
13+
function f(x) {
14+
assert.equal(Date.now() - start, 1000);
15+
}
16+
f = sinon.spy(f);
17+
18+
var f1000 = delay(f, 1000);
19+
f1000("test");
20+
this.clock.tick(2000);
21+
assert(f.calledOnce, 'calledOnce check fails');
22+
});
23+
24+
it("передаёт аргументы и контекст", function() {
25+
var start = Date.now();
26+
var user = {
27+
sayHi: function(phrase, who) {
28+
assert.equal(this, user);
29+
assert.equal(phrase, "Привет");
30+
assert.equal(who, "Вася");
31+
assert.equal(Date.now() - start, 1500);
32+
}
33+
};
34+
35+
user.sayHi = sinon.spy(user.sayHi);
36+
37+
var spy = user.sayHi;
38+
user.sayHi = delay(user.sayHi, 1500);
39+
40+
user.sayHi("Привет", "Вася");
41+
42+
this.clock.tick(2000);
43+
44+
assert(spy.calledOnce, 'проверка calledOnce не сработала');
45+
});
46+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
```js run
4+
function delay(f, ms) {
5+
6+
*!*
7+
return function() {
8+
setTimeout(() => f.apply(this, arguments), ms);
9+
};
10+
*/!*
11+
12+
}
13+
14+
function f(x) {
15+
alert(x);
16+
}
17+
18+
let f1000 = delay(f, 1000);
19+
let f1500 = delay(f, 1500);
20+
21+
f1000("test"); // shows "test" after 1000ms
22+
f1500("test"); // shows "test" after 1500ms
23+
```
24+
25+
Arrow function makes it easy, because `setTimeout` uses arguments and context of the wrapper .
26+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
importance: 5
2+
3+
---
4+
5+
# Delaying decorator [todo move after arrow]
6+
7+
Create a decorator `delay(f, ms)` that delays each call of `f` by `ms` milliseconds.
8+
9+
For instance:
10+
11+
```js
12+
function f(x) {
13+
alert(x);
14+
}
15+
16+
// create wrappers
17+
let f1000 = delay(f, 1000);
18+
let f1500 = delay(f, 1500);
19+
20+
f1000("test"); // shows "test" after 1000ms
21+
f1500("test"); // shows "test" after 1500ms
22+
```
23+
24+
In other words, `delay(f, ms)` returns a "delayed by `ms`" variant of `f`.
25+
26+
In the code above, `f` is a function of a single argument, but your solution should pass all arguments and the context `this`.
27+

1-js/7-deeper/6-call-apply-decorators/1-logging-decorator/_js.view/solution.js

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

1-js/7-deeper/6-call-apply-decorators/1-logging-decorator/_js.view/test.js

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

1-js/7-deeper/6-call-apply-decorators/1-logging-decorator/solution.md

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

1-js/7-deeper/6-call-apply-decorators/1-logging-decorator/task.md

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

1-js/7-deeper/6-call-apply-decorators/2-logging-decorator-arguments/_js.view/solution.js

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

0 commit comments

Comments
 (0)