Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda