Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
Proxy trap for a part of functions

I implemented a proxy handler to intercept all functions in a class.

class SBFinder {
  foo1({ name, id }) {
    console.log(name + id);
  }

  foo2({ name, id }) {
    console.log(id + name);
  }
}

const handler = {
  get(target, propKey, receiver) {
    const targetValue = Reflect.get(target, propKey, receiver);
    if (typeof targetValue === 'function') {
      return (...args) => {
        console.log('before');
        const res = targetValue.apply(this, args);
        console.log('after');
        return res;
      };
    }

    return targetValue;
  },
};

const finderProxy = new Proxy(new SBFinder(), handler);
finderProxy.foo1({ name: 'name1', id: 223 });

Now I want the handler to be applied only to a part of funstions. I understand that it's quite easy to implement according to a function name, number of arguments etc. But I don't want to limit the user by a naming conventions. Is there a way to do it for example according to some JSDocs tag (like we do with annotations in java). Or maybe there's another practice you can advice?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

can you be more specific in what you want to do?

In your get handler you are getting the class methods ("functions") but you should not call the function here.

if you return a wrapper function then you should make sure that you bind the correct object (which is the target not this) and you will want some logic to evaluate the args or the method name.

The desired method name is what you have referred to a propKey. So you could have some logic that examines propKey.

Protip -- if you are going to do tests against the value of propKey then watch out that propKey could be a symbol so you might have to add a check for when propKey is a string (e.g. a regex test will auto-cast to string)

Note also that you could return a proxy for the function also like this:

const fnProxyMaker = (fn)=>new Proxy( fn, { 
   apply(target, that, args){
       // do something before calling or whatever
      return target(...args);
   }
});

const handler = {
  get(target, propKey, receiver) {
    const targetValue = Reflect.get(target, propKey, receiver);
    if (typeof targetValue === 'function' && propKey === 'foo1') {
      return fnProxyMaker(targetValue)
    }

    return targetValue;
  },
};

or define the methods as proxies in the class:

class SBFinder {
  constructor(){
     ['foo1','foo2'].forEach(method=>
        Object.defineProperty(this, method, {
         get : fnProxyMaker( ({name,id})=>console.log(name+id) )
      }), this )
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!