...

/

Solution Review: Revocable Proxy

Solution Review: Revocable Proxy

Let's discuss the solution to the revocable proxy exercise given in the previous lesson.

We'll cover the following...

Solution

Look at how the code given below solves the challenge.

Press + to interact
'use strict';
const createRevocableProxy = function(instance) {
const handler = {
get: function(target, propertyName , receiver) {
return Reflect.get(target, propertyName).bind(target);
}
};
const { proxy, revoke } = Proxy.revocable(instance, handler);
setTimeout(revoke, 3000);
return proxy;
};
const proxy = createRevocableProxy(new Date());
const callGetYear = function() {
try {
console.log(1900 + proxy.getYear());
} catch(ex) {
console.log(ex.message);
}
};
callGetYear(); //current year like 2018
setTimeout(callGetYear, 1000);
setTimeout(callGetYear, 5000);
//Cannot perform 'get' on a proxy that has been revoked

Explanation

Now, let’s dig into the above code.

Rember you were asked to create and return a proxy from the function createRevocableProxy().

handler

First of all, we create a handlerHandler to ...