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

304
Views
Cómo desestructurar los métodos de una clase usando proptypes sin perder (este) contexto

Me pregunto si es posible desestructurar las propiedades/métodos de una instancia de una clase o función manteniendo el alcance en las variables desestructuradas sin tener que usar call() cada vez que necesito ese método. Por ejemplo:

 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!!

Quiero evitar la llamada () y no puedo refactorizar y poner todos los métodos dentro de la clase porque tengo un montón de ellos, quiero saber si existe alguna solución más inteligente para esto.

Solución :

Vincular "esto" en el 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!