How does this line work in the decorator?
slow = cachingDecorator (slow);
Why, when we assign "slow" as an argument, the function "slow" itself continues to work in its original form? That is, why isn't it being reassigned? Does it have something to do with closure? Please tell me what this technique is called and what to google about this topic.
To begin with,
slowis reassigned. Initially, the function does something. and after that, caching is added before it.
That is, after
slow = cachingDecorator (slow);in
slowAnother function is found.
is the "slow" function still working as it was?
Because after the rewriting,
slowcontains a function that has access to the
funcvariable (it stores the original functionality). And the new
slowchecks the cache and if it is empty starts
Sorry if I didn't explain it clearly :)Anonymous
yes, this is due to closure, at the time of the creation of the anonymous function, this anonymous function remembers external variables, in this case the slow function, which is stored in func, wherever this anonymous function is called, it calls the original slow function. Read it from Cantor.Anonymous
Why, when we assign "slow" as an argument, does the "slow" function itself continue to work in its original form?
let result = func (x); // otherwise, call the functionAnonymous
To place the code, please use CodePen or similar tool. Thanks you!