Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda