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

302
Views
How destructuring a class methods using proptypes without lose (this) context

i'm wondering if it's possible to destructure the properties/methods from an instance of a class or function while maintaining scope across the destructured variables without having to use call() every time i need that method? For example:

class Pearson {}

Pearson.prototype.speak = function speak(){
  return this.sayHi
};

Pearson.prototype.speakOutLoud = function speakOutLoud(){
  return this.speak().toUpperCase()
};

const pearson = Object.assign(new Pearson(), {sayHi:"hi!!"});

const {speak, speakOutLoud} = pearson;

speak.call(pearson) // hi!!
speakOutLoud.call(pearson) // HI!!

I want to avoid the call() and i can't refactor and put all methods inside the class because i have a bunch of them, want to know if exist any cleaver solution to this.

Solution:

Bind "this" in the constructor

class Pearson {
    constructor(){
        this.speak = this.speak.bind(this)
        this.speakOutLoud = this.speakOutLoud.bind(this)
    }
}

Pearson.prototype.speak = function speak(){
  return this.sayHi
};

Pearson.prototype.speakOutLoud = function speakOutLoud(){
  return this.speak().toUpperCase()
};

const {speak} = Object.assign(new Pearson(), {sayHi:"hi!!"});

speak() // "hi!!"
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!