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

115
Views
JS Call new Class().function1().function2() and keep "this"

I have a problem with Class Javascript. I created class with constructor and two functions.

export default class Example {
  private image: string;
  private processedImage: Sharp | undefined;

  constructor(image: string) {
    this.image = image;
  }

  resize(): ResizeClb {
    this.processedImage = resizeImage(this.image, ...);

    return {
      merge: this.merge
    };
  }

  merge(images: MergeImages[]): void {
    console.log(this.processedImage); // print undefined ๐Ÿ‘ˆ๐Ÿ‘ˆ๐Ÿ‘ˆ
    this.processedImage = this.processedImage.composite(layers);
  }
}

I want call chained functions:

const a = new Example(myImage).resize().merge(...othersImages);

But when I called the chained function I get undefined in this.processedImage line. I can't access to value of the class.

It's work if I not use chains functions.

const a = new Example(myImage);
a.resize();
a.merge(...othersImages); // It work.

It work but I required use chains functions. Thank you!

about 4 years ago ยท Juan Pablo Isaza
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!