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

272
Views
¿Hay alguna forma de proxy (interceptar) todos los métodos de una clase en javascript?

Quiero poder representar todos los métodos de una clase dentro del constructor de una clase en sí.

 class Boy { constructor() { // proxy logic, do something before each call of all methods inside class // like if arg passed is 3, print something additionally } run(meters) { console.log(meters) } walk(meters) { // walk } } const myBoy = new Boy(); console.log(myBoy.run(3)) // should print 3 and something else

Creo que un bucle for para cada método sería un enfoque interesante, pero en ese momento podría implementar la lógica en las primeras líneas de cada función.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Me di cuenta de que podía crear un proxy con el objetivo como el objeto de clase en sí mismo y luego indexar el método.

 class Boy { constructor() { // proxy logic, do something before each call of all methods inside class // like if arg passed is 3, print something additionally return new Proxy(this, { get(target, prop) { const origMethod = target[prop]; if (typeof origMethod == 'function') { return function (...args) { if (args[0] == 3) { return "3 is unlucky, you didn't go anywhere." } let result = origMethod.apply(target, args) return result } } } }) } run(meters) { return `you ran ${meters}!` } walk(meters) { return `you walked ${meters}!` // walk } } const myBoy = new Boy(); console.log(myBoy.run(2)) // prints "you ran 2!" console.log(myBoy.walk(3)) // prints "3 is unlucky, you didn't run." console.log(myBoy.run(3)) // prints "3 is unlucky, you didn't run."
about 4 years ago · Juan Pablo Isaza 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!