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

269
Visualizações
How do I intercept sort function within a JS proxy?

I know how to intercept get`set` and such within a proxy using the following code:

function get( target, prop, receiver ) {
  console.log("Intercepted get")
  return Reflect.get( target, prop, receiver );
}
var handler = {
  'get': get
};
var proxy = new Proxy( new Array(...[7, 1,2,3,4,5]), handler );

I'm unable to replicate this for sort. Adding:

function sort(target, prop) {
  console.log("Intercepted sort")
  return Reflect.sort(target);
}
var handler = {
  'get': get,
  'sort': sort
};

Doesn't work. I've also been unable to find documentation. How do I intercept the sort function?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to use get to trap methods. get is called for each property and each method that is read. The name of the property or method is passed into get as second argument prop:

function sort() {
  console.log("Intercepted sort")
  return this.sort();
}
function get( target, prop, receiver ) {
  if (prop === 'sort') {
    return sort.bind(target);
  }
  console.log("Intercepted get")
  return Reflect.get( target, prop, receiver );
}
var handler = {
  get
};
var proxy = new Proxy( new Array(...[7,1,2,3,4,5]), handler );
console.log(proxy.sort())

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