Skip to content

Commit b2cedf7

Browse files
committed
closes #1308
1 parent c01347a commit b2cedf7

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

1-js/06-advanced-functions/09-call-apply-decorators/article.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ To summarize, there are several benefits of using a separate `cachingDecorator`
6161
- The caching logic is separate, it did not increase the complexity of `slow` itself (if there was any).
6262
- We can combine multiple decorators if needed (other decorators will follow).
6363

64-
6564
## Using "func.call" for the context
6665

6766
The caching decorator mentioned above is not suited to work with object methods.
@@ -168,10 +167,8 @@ let user = { name: "John" };
168167
say.call( user, "Hello" ); // John: Hello
169168
```
170169

171-
172170
In our case, we can use `call` in the wrapper to pass the context to the original function:
173171

174-
175172
```js run
176173
let worker = {
177174
someMethod() {
@@ -393,12 +390,20 @@ Taken from the specification almost "as-is":
393390

394391
So, technically it takes `this` and joins `this[0]`, `this[1]` ...etc together. It's intentionally written in a way that allows any array-like `this` (not a coincidence, many methods follow this practice). That's why it also works with `this=arguments`.
395392

393+
## Decorators and function properties
394+
395+
It is generally safe to replace a function or a method with a decorated one, except for one little thing. If the original function had properties on it, like `func.calledCount` or whatever, then the decorated one will not provide them. Because that is a wrapper. So one needs to be careful if one uses them.
396+
397+
E.g. in the example above if `slow` function had any properties on it, then `cachingDecorator(slow)` is a wrapper without them.
398+
399+
Some decorators may provide their own properties. E.g. a decorator may count how many times a function was invoked and how much time it took, and expose this information via wrapper properties.
400+
401+
There exists a way to create decorators that keep access to function properties, but this requires using a special `Proxy` object to wrap a function. We'll discuss it later in the article <info:proxy#proxy-apply>.
402+
396403
## Summary
397404

398405
*Decorator* is a wrapper around a function that alters its behavior. The main job is still carried out by the function.
399406

400-
It is generally safe to replace a function or a method with a decorated one, except for one little thing. If the original function had properties on it, like `func.calledCount` or whatever, then the decorated one will not provide them. Because that is a wrapper. So one needs to be careful if one uses them. Some decorators provide their own properties.
401-
402407
Decorators can be seen as "features" or "aspects" that can be added to a function. We can add one or add many. And all this without changing its code!
403408

404409
To implement `cachingDecorator`, we studied methods:

1-js/99-js-misc/01-proxy/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ alert(50 in range); // false
497497

498498
A nice syntactic sugar, isn't it? And very simple to implement.
499499

500-
## Wrapping functions: "apply"
500+
## Wrapping functions: "apply" [#proxy-apply]
501501

502502
We can wrap a proxy around a function as well.
503503

0 commit comments

Comments
 (0)