You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/09-call-apply-decorators/article.md
+10-5
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,6 @@ To summarize, there are several benefits of using a separate `cachingDecorator`
61
61
- The caching logic is separate, it did not increase the complexity of `slow` itself (if there was any).
62
62
- We can combine multiple decorators if needed (other decorators will follow).
63
63
64
-
65
64
## Using "func.call" for the context
66
65
67
66
The caching decorator mentioned above is not suited to work with object methods.
@@ -168,10 +167,8 @@ let user = { name: "John" };
168
167
say.call( user, "Hello" ); // John: Hello
169
168
```
170
169
171
-
172
170
In our case, we can use `call` in the wrapper to pass the context to the original function:
173
171
174
-
175
172
```js run
176
173
let worker = {
177
174
someMethod() {
@@ -393,12 +390,20 @@ Taken from the specification almost "as-is":
393
390
394
391
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`.
395
392
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
+
396
403
## Summary
397
404
398
405
*Decorator* is a wrapper around a function that alters its behavior. The main job is still carried out by the function.
399
406
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
-
402
407
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!
403
408
404
409
To implement `cachingDecorator`, we studied methods:
0 commit comments