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

194
Views
Modify Class Method Definition in TS or JS

I have a class having structure like :

class A {
    constructor() {}
    myMethod() {
      console.log('in my method');
    }
}

I want to make a method that will accept className and methodName like :

modifyClassMethod(className, methodName)

This method should wrap the whole body of methodName specified into a callback during runtime.

So if I do,

modifyClassMethod(A, myMethod)

Now during runtime the body of myMethod of class A should get changed to

myMethod() {
   nr.recordMe('here', {
      console.log('in my method').
   })
} 

Now when I will create a new object of A class, then I will get modified value of myMethod.

How can I achieve this in TS or JS ?.

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

0

You can get help of Decorator Design Pattern. It's also called as wrapper. In Typescript you can do;

interface IClassInterface{
    myMethod():void
}

class classA implements IClassInterface{
    constructor() {}
    myMethod() {
      console.log('in my method');
    }
}

class AWrapper implements IClassInterface{
  constructor(private classA:IClassInterface){}
    myMethod(somestring?:string) {
      //this.classA.myMethod(); either do this, or do sth else
      console.log("wrapper works", somestring);
      this.classA.myMethod();
    }
}

and when using;

let aClass = new classA();
let IClassWrapper =  new AWrapper(aClass);
IClassWrapper.myMethod('here');
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!