Exercise on Proxies
Using your knowledge of proxies object, you must use them to: i) log the number of times a function is accessed ii) create a revocable discount object
We'll cover the following...
Exercise 1:
Suppose the following fibonacci
implementation is given:
fibonacci = n =>
n <= 1 ? n :
fibonacci( n - 1 ) + fibonacci( n - 2 );
Determine how many times the fibonacci
function is called when evaluating fibonacci( 12 )
.
Determine how many times fibonacci
is called with the argument 2
when evaluating fibonacci( 12 )
.
Press + to interact
let fibonacci = n =>n <= 1 ? n : fibonacci( n - 1 ) + fibonacci( n - 2 ); //the fibonacci functionlet fibCalls = 0; // total calls to the fibonacci functionlet fibCallsWith2 = 0; // calls to the fibonacci function with 2 as an argument//Write your code here// Do not edit code below this lineconsole.log( 'fibCalls:', fibCalls );console.log( 'fibCallsWith2:', fibCallsWith2 );
Explanation:
The solution is not that hard. As we learned earlier,
fibonacci = new Proxy( fibonacci, {
uses the same reference name as our fibonacci function ...