Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators and forwarding, call/apply #218

Merged
Show file tree
Hide file tree
Changes from 136 commits
Commits
Show all changes
139 commits
Select commit Hold shift + click to select a range
6f24e24
add portuguese translations for 09-call-apply-decorators
nazarepiedady Jul 20, 2021
f61fc2a
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 24, 2021
75233f9
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 24, 2021
df22c3a
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 24, 2021
f791dc2
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/s…
nazarepiedady Jul 24, 2021
f21bfd1
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/s…
nazarepiedady Jul 24, 2021
ed2891f
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
e81827a
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/s…
nazarepiedady Jul 24, 2021
5a1d081
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/s…
nazarepiedady Jul 24, 2021
65061ee
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
a940f62
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
beb803e
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
8daf648
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
e035343
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
e64df68
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/s…
nazarepiedady Jul 24, 2021
2ac8f34
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/t…
nazarepiedady Jul 24, 2021
1fe7c8e
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/t…
nazarepiedady Jul 24, 2021
6ec0c37
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
27720a9
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
3c51f58
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
019f8c7
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
470c042
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
2424a49
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
373d7de
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
f4eb016
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
28db3ae
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
fc60987
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/t…
nazarepiedady Jul 24, 2021
31c7c26
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
9922f99
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/t…
nazarepiedady Jul 24, 2021
ce1c564
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
20201dd
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
79cb3d9
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
fe0ac71
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
e44b0a2
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
5408986
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
5a7fb16
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
febace3
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
2a1a6cc
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
a821f52
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
0e02ffa
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
e18716d
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
6fb0673
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
26310a2
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
60dd4ea
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
960a729
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 24, 2021
5b97fc8
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
3351efc
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
08b2eb7
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
94d26d6
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
42c670d
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
30ae0a0
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
3265629
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
7267678
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
f3e93b3
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
b5625b9
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
ed8f906
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
045b955
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
a0fe4a0
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
3d31814
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
2f07e4b
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
bf58b55
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
3303ae5
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
fb1db91
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
0493aa4
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
73f53ea
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
741ff03
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 24, 2021
049b00d
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 24, 2021
da7eff1
Apply suggestions from code review
nazarepiedady Jul 24, 2021
f98ef4b
translate to portuguese debounce.view/index.html
nazarepiedady Jul 24, 2021
8b3c159
Update task.md
odsantos Jul 25, 2021
d16f1fa
Add a blank line.
odsantos Jul 26, 2021
50cd890
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 26, 2021
08c2184
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 26, 2021
8c800bd
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 26, 2021
4ec2b71
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 26, 2021
9034888
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/t…
nazarepiedady Jul 26, 2021
17d85a6
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/t…
nazarepiedady Jul 26, 2021
193db3c
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 26, 2021
034ee04
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 26, 2021
1f12b61
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Jul 26, 2021
fa16310
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 26, 2021
1eab016
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 26, 2021
d5c26ae
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 26, 2021
b492500
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 26, 2021
f483b32
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 26, 2021
11f826b
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 26, 2021
833f4b0
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 26, 2021
8f77aa3
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 26, 2021
8865725
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 26, 2021
1dad4e7
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 26, 2021
8434772
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 26, 2021
8e0dae4
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 26, 2021
2337bb9
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/s…
nazarepiedady Jul 26, 2021
c32f813
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/t…
nazarepiedady Jul 26, 2021
bf26d67
Add space for untranslated line.
odsantos Jul 27, 2021
0d241d6
Add untranslated line.
odsantos Jul 27, 2021
18e8949
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Jul 28, 2021
029fe81
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/s…
nazarepiedady Jul 28, 2021
d13f456
translate the scripts into 4-throttle folder
nazarepiedady Jul 28, 2021
0b65ff8
translate the scripts into 3-debounce folder
nazarepiedady Jul 28, 2021
1ad0da4
translate the scripts into 01-spy-decorator folder
nazarepiedady Jul 28, 2021
deb74e8
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 29, 2021
2ddace6
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 29, 2021
c86916e
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Jul 29, 2021
fdb8eb2
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 29, 2021
71a229f
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 29, 2021
fbfd1a0
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 29, 2021
16a516b
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Jul 29, 2021
3fe9057
complete translations that are not completed
nazarepiedady Jul 29, 2021
41512ab
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Aug 2, 2021
af56b83
Update 1-js/06-advanced-functions/09-call-apply-decorators/02-delay/_…
nazarepiedady Aug 4, 2021
55e6731
Update 1-js/06-advanced-functions/09-call-apply-decorators/01-spy-dec…
nazarepiedady Aug 4, 2021
c6148f9
Update 1-js/06-advanced-functions/09-call-apply-decorators/03-debounc…
nazarepiedady Aug 4, 2021
d05874c
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Aug 4, 2021
c4a1750
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Aug 4, 2021
3996f37
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Aug 4, 2021
23b6c11
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Aug 4, 2021
7969605
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Aug 4, 2021
9bf4f1f
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Aug 4, 2021
b1da41a
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Aug 4, 2021
fd1e970
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Aug 4, 2021
dc5e050
Update 1-js/06-advanced-functions/09-call-apply-decorators/article.md
nazarepiedady Aug 4, 2021
9f06223
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Aug 4, 2021
b126223
Update 1-js/06-advanced-functions/09-call-apply-decorators/04-throttl…
nazarepiedady Aug 4, 2021
2deb617
docs: update `call-apply-decorators/throttle` article
nazarepiedady Mar 5, 2024
553b999
docs: update `call-apply-decorators/debounce` article
nazarepiedady Mar 5, 2024
58420f8
docs: update `call-apply-decorators/delay` article
nazarepiedady Mar 5, 2024
626cf58
docs: update `call-apply-decorators/debounce/debounce.svg`
nazarepiedady Mar 5, 2024
bf3e634
docs: update `call-apply-decorators/decorator-makecaching-wrapper.svg`
nazarepiedady Mar 5, 2024
07f5a47
docs: update `call-apply-decorators/spy-decorator` article
nazarepiedady Mar 15, 2024
5a2cc59
docs: update `call-apply-decorators/article.md` english content
nazarepiedady Mar 15, 2024
8fc6d38
docs: translate the first two section of `call-apply-decorators/artic…
nazarepiedady Mar 15, 2024
23ed1bf
docs: translate one more piece of `call-apply-decorators` article
nazarepiedady Mar 16, 2024
fe966ce
docs: translate one more piece of `call-apply-decorators` article
nazarepiedady Mar 17, 2024
a75c9f4
docs: finish the translation of `call-apply-decorators` article
nazarepiedady Mar 18, 2024
00481c7
Merge branch 'master' into decorators-forwarding-call-apply
nazarepiedady Mar 18, 2024
79b2b8c
docs: update `call-apply-decorators/spy-decorator/task.md`
nazarepiedady Mar 24, 2024
f60fbb7
docs: update `call-apply-decorators/delay/solution.md`
nazarepiedady Mar 24, 2024
9f2d639
docs: update `call-apply-decorators/spy-decorator/task.md`
nazarepiedady Mar 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function spy(func) {

function wrapper(...args) {
// using ...args instead of arguments to store "real" array in wrapper.calls
// usar `...args` ao invés de `arguments`
// para armazenar o vetor "real" no `wrapper.calls`
wrapper.calls.push(args);
return func.apply(this, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function spy(func) {
// your code
// o teu código
}


Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ describe("spy", function() {
});

it("transparently wraps functions", function() {

let sum = sinon.spy((a, b) => a + b);

let wrappedSum = spy(sum);

assert.equal(wrappedSum(1, 2), 3);
assert(sum.calledWith(1, 2));
assert(sum.calledWidth(1, 2));
});


it("transparently wraps methods", function() {

let calc = {
sum: sinon.spy((a, b) => a + b)
};
Expand All @@ -40,5 +39,4 @@ describe("spy", function() {
assert(calc.sum.calledWith(1, 2));
assert(calc.sum.calledOn(calc));
});

});
});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The wrapper returned by `spy(f)` should store all arguments and then use `f.apply` to forward the call.
O embrulhador retornado pela `spy(f)` deve armazenar todos os argumentos e então usar `f.apply` para encaminhar a chamada.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Spy decorator
# Decorador de Espionagem

Create a decorator `spy(func)` that should return a wrapper that saves all calls to function in its `calls` property.
Cria um decorador `spy(func)` que retorna um embrulhador que guarde todas as chamadas à função na sua propriedade `calls`.

Every call is saved as an array of arguments.
Toda chamada é guardada num vetor de argumentos.

For instance:
Por exemplo:

```js
function work(a, b) {
alert( a + b ); // work is an arbitrary function or method
alert( a + b ); // `work` é uma função ou método arbitrário
}

*!*
Expand All @@ -27,4 +27,4 @@ for (let args of work.calls) {
}
```

P.S. That decorator is sometimes useful for unit-testing. Its advanced form is `sinon.spy` in [Sinon.JS](http://sinonjs.org/) library.
Pós-escrito: Este decorador é algumas vezes útil para testes unitários. Sua forma avançada é `sinon.spy` na biblioteca [Sinon.JS](http://sinonjs.org).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Pós-escrito: Este decorador é algumas vezes útil para testes unitários. Sua forma avançada é `sinon.spy` na biblioteca [Sinon.JS](http://sinonjs.org).
P.S. Esse decorador pode eventualmente ser útil para testes unitários. Sua forma avançada é `sinon.spy` na biblioteca [Sinon.JS](http://sinonjs.org).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fique a vontade para manter o "Pós-escrito" :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Peruibeloko, I need to keep it because it helps increase the comprehension of the content.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("delay", function() {
this.clock.restore();
});

it("calls the function after the specified timeout", function() {
it("chama a função após o tempo de espera especificado", function() {
let start = Date.now();

function f(x) {
Expand All @@ -16,12 +16,12 @@ describe("delay", function() {
f = sinon.spy(f);

let f1000 = delay(f, 1000);
f1000("test");
f1000("teste");
this.clock.tick(2000);
assert(f.calledOnce, 'calledOnce check fails');
assert(f.calledOnce, 'a verificação de calledOnce falha');
});

it("passes arguments and this", function() {
it("passa os argumentos e o contexto de this", function() {
let start = Date.now();
let user = {
sayHi: function(phrase, who) {
Expand All @@ -37,10 +37,10 @@ describe("delay", function() {
let spy = user.sayHi;
user.sayHi = delay(user.sayHi, 1500);

user.sayHi("Hello", "John");
user.sayHi("Olá", "John");

this.clock.tick(2000);

assert(spy.calledOnce, 'calledOnce check failed');
assert(spy.calledOnce, 'a verificação de calledOnce falhou');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The solution:
A solução:

```js run demo
function delay(f, ms) {
Expand All @@ -11,22 +11,22 @@ function delay(f, ms) {

let f1000 = delay(alert, 1000);

f1000("test"); // shows "test" after 1000ms
f1000("teste"); // exibe "teste" após 1000ms
```

Please note how an arrow function is used here. As we know, arrow functions do not have own `this` and `arguments`, so `f.apply(this, arguments)` takes `this` and `arguments` from the wrapper.
Nota como é utilizada aqui uma função de seta (ou função anónima). Como sabemos, as funções de seta não possuem `this` e `arguments` próprios, então `f.apply(this, arguments)` recebe o `this` e `arguments` da função envolvente.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Nota como é utilizada aqui uma função de seta (ou função anónima). Como sabemos, as funções de seta não possuem `this` e `arguments` próprios, então `f.apply(this, arguments)` recebe o `this` e `arguments` da função envolvente.
Note como é utilizada aqui uma função de seta. Como sabemos, as funções de seta não possuem `this` e `arguments` próprios, então `f.apply(this, arguments)` recebe o `this` e `arguments` da função envolvente.

Funções de seta e funções anônimas não são conceitos equivalentes, funções de seta podem ser nomeadas e funções anônimas podem ser declaradas usando function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Peruibeloko, I think you are not remembering that anonymous functions could be named.

To illustrate what I want to tell, consider this humble example below:

let getName = function(name) { return name }
console.log(`${getName('Marcos')} was called by ${getName.name} function`)

getName = (name) => name
console.log(`${getName('Marcos')} was called by ${getName.name} function`)

I recommend you to try to run the code above to see what I am talking about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nazarepiedady On your example, neither function is anonymous:

image

  • The first one creates an anonymous function, which then gets named as getName1
  • The second one creates a named arrow function called getName2.

An anonymous function is, by definition, a function without a name.

A better example would be:

// Anonymous function expression
console.log((function() {}).name)

// Named function declaration
console.log((function namedDeclaration() {}).name)

// Anonymous arrow function
console.log((() => {}).name)

// Named arrow function
const namedArrow = () => {}
console.log(namedArrow.name)

Which correctly prints out:

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Peruibeloko, You are absolutely correct, I was not remembered about this approach.


If we pass a regular function, `setTimeout` would call it without arguments and `this=window` (assuming we're in the browser).
Se passarmos uma função normal, `setTimeout` a chamaria sem argumentos e `this=window` (assumindo que estamos no navegador).

We still can pass the right `this` by using an intermediate variable, but that's a little bit more cumbersome:
Nós podemos ainda passar o `this` correto utilizando uma variável intermediária, mas isso é um pouco mais complicado:

```js
function delay(f, ms) {

return function(...args) {
let savedThis = this; // store this into an intermediate variable
let savedThis = this; // armazenar isto numa variável intermédia
setTimeout(function() {
f.apply(savedThis, args); // use it here
f.apply(savedThis, args); // utilizá-lo aqui
}, ms);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ importance: 5

---

# Delaying decorator
# Decorador de Atraso

Create a decorator `delay(f, ms)` that delays each call of `f` by `ms` milliseconds.
Crie um decorador `delay(f, ms)` que atrasa cada chamada de `f` por `ms` milissegundos.

For instance:
Por exemplo:

```js
function f(x) {
alert(x);
}

// create wrappers
// criar funções envolventes
let f1000 = delay(f, 1000);
let f1500 = delay(f, 1500);

f1000("test"); // shows "test" after 1000ms
f1500("test"); // shows "test" after 1500ms
f1000("teste"); // exibe "teste" após 1000ms
f1500("teste"); // exibe "teste" após 1500ms
```

In other words, `delay(f, ms)` returns a "delayed by `ms`" variant of `f`.
Por outras palavras, `delay(f, ms)` retorna uma variante "atrasada por `ms`" de `f`.

In the code above, `f` is a function of a single argument, but your solution should pass all arguments and the context `this`.
No código acima, `f` é uma função de um único argumento, mas a sua solução deve passar todos os argumentos e o contexto de `this`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ describe('debounce', function () {
this.clock.restore();
});

it('for one call - runs it after given ms', function () {
it('para uma chamada - execute-a após um determinado ms', function () {
const f = sinon.spy();
const debounced = debounce(f, 1000);

debounced('test');
assert(f.notCalled, 'not called immediately');
assert(f.notCalled, 'não é chamada imediatamente');
this.clock.tick(1000);
assert(f.calledOnceWith('test'), 'called after 1000ms');
assert(f.calledOnceWith('test'), 'chamada após 1000ms');
});

it('for 3 calls - runs the last one after given ms', function () {
it('para 3 chamadas - executa a última após determinados ms', function () {
const f = sinon.spy();
const debounced = debounce(f, 1000);

debounced('a');
setTimeout(() => debounced('b'), 200); // ignored (too early)
setTimeout(() => debounced('c'), 500); // runs (1000 ms passed)
setTimeout(() => debounced('b'), 200); // ignorada (demasiado cedo)
setTimeout(() => debounced('c'), 500); // executar (1000ms passados)
this.clock.tick(1000);

assert(f.notCalled, 'not called after 1000ms');
assert(f.notCalled, 'não é chamada após 1000ms');

this.clock.tick(500);

assert(f.calledOnceWith('c'), 'called after 1500ms');
assert(f.calledOnceWith('c'), 'chamada após 1500ms');
});

it('keeps the context of the call', function () {
it('mantém o contexto da chamada', function () {
let obj = {
f() {
assert.equal(this, obj);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<!doctype html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Function <code>handler</code> is called on this input:
A função <code>handler</code> é chamada nesta entrada:
<br>
<input id="input1" placeholder="type here">

<p>

Debounced function <code>debounce(handler, 1000)</code> is called on this input:
A função <code>debounce(handler, 1000)</code> reduzida é chamada nesta entrada:
<br>
<input id="input2" placeholder="type here">

<p>
<button id="result">The <code>handler</code> puts the result here</button>
<button id="result">A <code>handler</code> coloca o resultado aqui</button>

<script>
function handler(event) {
Expand All @@ -21,4 +21,4 @@

input1.oninput = handler;
input2.oninput = _.debounce(handler, 1000);
</script>
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ function debounce(func, ms) {

```

A call to `debounce` returns a wrapper. When called, it schedules the original function call after given `ms` and cancels the previous such timeout.

Uma chamada à `debounce` retorna um embrulhador. Quando chamado, este agenda a chamada da função original depois de dados `ms` e cancela o tempo de espera anterior.
Loading