vendredi 2 avril 2021

Javascript - Can the Proxy pattern be used to add extra functionality?

I commonly use the Proxy pattern to avoid API calls

const bankAPI = () => ({
   getUserData: () => { name: "Victorio" },
})

function bankAPIProxy = () => {
   const bankAPI = bankAPI();
   const cache = {};
 
   getUserData = () => {
      if(cache.userData) return cache.userData; // Save API Calls
      cache.userData = bankAPI.getUserData();
   }
}

But, can this same pattern be used to add extra functionality and still be considered a Proxy?

What about, if instead of implementing that, the Proxy looks like this:

function bankAPIProxy = () => {
   const bankAPI = bankAPI();
 
   getUserData = () => bankAPI.getUserData();

   newMethod = () => "Hello World!";
}

is this still a proxy?

Aucun commentaire:

Enregistrer un commentaire