Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
¿Hay alguna manera de forzar una función para que falle globalmente en JavaScript?

Me encontré con una situación en la que podría ser beneficioso arrojar un error cada vez que se llama a una función particular en un programa de JavaScript. Por lo cual, no me refiero a una referencia a la función, sino a todas y cada una de las referencias a la función, es decir, la función misma.

 let doThing = ()=>console.log("Hello, world!"); // refer to the thing doThing refers to let a = doThing; // change what doThing refers to doThing = ()=>{throw new Error()}; // This won't work for my use case // "Hello, world!" a(); // Because a still points to the working function let b = a; // kill what a points to killFunctionSymbol(a); // Instead, I want something like this // After which, everything fails... a(); // Error b(); // Error

He leído en otra parte que es imposible mutar el cuerpo de una función. Pero, en mi caso, no estoy seguro de que necesite mutar el cuerpo. Solo necesito la llamada al error. ¿Hay alguna manera de que pueda lograr esto? ¿Hay alguna forma en que pueda alterar el prototipo para que siempre se rompa?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Creo que lo que quieres es un Proxy. Debo decir que siempre estoy atento al uso de proxies, por lo que este es un caso de uso perfecto.

Aquí hay una implementación muy básica de un proxy que secuestra la llamada a una función basada en algún valor externo

 const doThing = (arg1, arg2) => `Hello World! ${arg1} ${arg2}`; let killSwitch = false; const handler = { apply: function(target, thisArg, argumentsList) { if (killSwitch) { throw new Error('Kill switch engaged'); } return target(...argumentsList); } }; const regular = doThing; const withProxy = new Proxy(doThing, handler); console.log(regular('foo1', 'bar1')); console.log(withProxy('foo2', 'bar2')); killSwitch = true; console.log(withProxy('foo3', 'bar3'));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

Espero que esto ayude.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!