-
Notifications
You must be signed in to change notification settings - Fork 112
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
Changes from 136 commits
6f24e24
f61fc2a
75233f9
df22c3a
f791dc2
f21bfd1
ed2891f
e81827a
5a1d081
65061ee
a940f62
beb803e
8daf648
e035343
e64df68
2ac8f34
1fe7c8e
6ec0c37
27720a9
3c51f58
019f8c7
470c042
2424a49
373d7de
f4eb016
28db3ae
fc60987
31c7c26
9922f99
ce1c564
20201dd
79cb3d9
fe0ac71
e44b0a2
5408986
5a7fb16
febace3
2a1a6cc
a821f52
0e02ffa
e18716d
6fb0673
26310a2
60dd4ea
960a729
5b97fc8
3351efc
08b2eb7
94d26d6
42c670d
30ae0a0
3265629
7267678
f3e93b3
b5625b9
ed8f906
045b955
a0fe4a0
3d31814
2f07e4b
bf58b55
3303ae5
fb1db91
0493aa4
73f53ea
741ff03
049b00d
da7eff1
f98ef4b
8b3c159
d16f1fa
50cd890
08c2184
8c800bd
4ec2b71
9034888
17d85a6
193db3c
034ee04
1f12b61
fa16310
1eab016
d5c26ae
b492500
f483b32
11f826b
833f4b0
8f77aa3
8865725
1dad4e7
8434772
8e0dae4
2337bb9
c32f813
bf26d67
0d241d6
18e8949
029fe81
d13f456
0b65ff8
1ad0da4
deb74e8
2ddace6
c86916e
fdb8eb2
71a229f
fbfd1a0
16a516b
3fe9057
41512ab
af56b83
55e6731
c6148f9
d05874c
c4a1750
3996f37
23b6c11
7969605
9bf4f1f
b1da41a
fd1e970
dc5e050
9f06223
b126223
2deb617
553b999
58420f8
626cf58
bf3e634
07f5a47
5a2cc59
8fc6d38
23ed1bf
fe966ce
a75c9f4
00481c7
79b2b8c
f60fbb7
9f2d639
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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 | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
The solution: | ||||||
A solução: | ||||||
|
||||||
```js run demo | ||||||
function delay(f, ms) { | ||||||
|
@@ -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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nazarepiedady On your example, neither function is anonymous:
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: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||
}; | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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" :)
There was a problem hiding this comment.
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.