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

236
Visualizações
How to "append" an action to a function?

I wanted to create a function that I can add actions to later. I tried this:

Function.prototype.appendAction = function(action) {
  let _ogFn = this
  return function(...args) {
    _ogFn(...args)
    action()
  }
}
function a() {
  console.log("foo")
}
a()
a = a.appendAction(() => console.log("bar"))
a()

This does work, but how can I make it change the function automatically? I want to make it work like this:

a()
a.appendAction(() => console.log("bar")) //note it doesn't have the "a = "
a() //changed function
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Depending on your scenario you could create a function decorator. This would mean defining a function (for example fnMod) that accepts a function fn and returns a decorated version that executes some before and after hooks.

const a = fnMod(() => {
  console.log("foo");
});

a();
a.appendAction(() => console.log("bar"));
a();



const john = {
  name: "John Doe",
  age: 42,
  greet: fnMod(function () {
    console.log(`I'm ${this.name}.`); // <- uses `this` so use a normal function
  })
}

john.greet.prependAction((name) => {
  console.log(`Hi ${name}!`); // <- does not use `this` so both a normal function
});                           //    or arrow function will work fine

john.greet.appendAction(function () {
  console.log(`I'm ${this.age} years old.`);
});

john.greet("Jane");



function fnMod(fn) {
  const before = [];
  const after = [];
  
  function hookable(...args) {
    before.forEach(fn => fn.apply(this, args));
    const result = fn.apply(this, args);
    after.forEach(fn => fn.apply(this, args));
    return result;
  };
  
  hookable.prependAction = (fn) => {
    before.unshift(fn);
  }
  
  hookable.appendAction = (fn) => {
    after.push(fn);
  }
  
  return hookable;
}

You might want to tweak it to your liking. Currently both this and all arguments passed to the decorated function are forwarded to all hooks, which might or might not be desirable.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you just want to call it immediately:

Function.prototype.appendAction = function(action) {
  let _ogFn = this
  return (function(...args) {
    _ogFn(...args)
    action()
  })()
}
function a() {
  console.log("foo")
}
a.appendAction(() => console.log("bar"))

That is the immediately invoked function expression (IIFE) pattern

about 4 years ago · Juan Pablo Isaza Relatório

0

For your purposes you could use eval:

First, grab the user input however you are handling it and parse it as a string.

Then use eval like so:

let userInput = 'console.log("hello world")';

const callUserFunction = (action) => {
  eval(`function fn(){${action}}`);
  fn();
}

callUserFunction(userInput);

Be aware that allowing a user to write or JS is a security risk, which can open doors to all kinds of attacks, so in most scenarios, I would discourage doing so

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