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

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

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 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