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

89
Visualizações
javascript catch function call

Can I catch (trigger event) when a certain function in a big, uneditable library is called?

// big js library
var biglibrary = {
    runme: function(arguments){
        // do something...
    },
    otherfunctions: function(.....
}

Now how can I outside above catch / notice when runme is called? Can I? I tried something like this ->

$(biglibrary).on('runme', function(){
    // function was called !!!
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In the vast majority of cases, you can wrap the function:

const original = biglibrary.runme;
biglibrary.runme = function(...args) {
    console.log("Function was called!");
    return original.apply(this, args);
};

const biglibrary = {
    runme: function(a, b) {
        console.log(`Original function: ${a} + ${b}`);
        return a + b;
    },
};

const original = biglibrary.runme;
biglibrary.runme = function(...args) {
    console.log("Function was called!");
    return original.apply(this, args);
}

console.log(biglibrary.runme(1, 2));

This is sometimes called "monkey-patching."

In some cases, the library may have made the runme property read-only, in which case you might be able to wrap it via Object.defineProperty:

const original = biglibrary.runme;
const descr = Object.getOwnPropertyDescriptor(biglibrary, "runme");
descr.value = function(...args) {
    console.log("Function was called!");
    return original.apply(this, args);
}
Object.defineProperty(biglibrary, "runme", descr);

const biglibrary = {
    runme: function(a, b) {
        console.log(`Original function: ${a} + ${b}`);
        return a + b;
    },
};

const original = biglibrary.runme;
const descr = Object.getOwnPropertyDescriptor(biglibrary, "runme");
descr.value = function(...args) {
    console.log("Function was called!");
    return original.apply(this, args);
}
Object.defineProperty(biglibrary, "runme", descr);

console.log(biglibrary.runme(1, 2));

If the library has made the property both read-only and non-configurable, you can't wrap it, but that's rare.

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