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

166
Views
Cómo referirse a 'esto' correcto en devolución de llamada anidada

En el siguiente código, obtengo una referencia indefinida para this.lister en el método create_components . Estoy tratando de entender el significado de this (aparentemente cambia según cómo llame al método), pero sería genial si alguien pudiera señalar la regla por la que this no se vincula con ScreenCreator y cómo puedo lograrlo.

¡Gracias!

 function ScreenCreator(config, container) { this.lister = new Lister(); this.goldenlayout = new GoldenLayout(config, container); this.create_components(); this.goldenlayout.init(); } ScreenCreator.prototype.create_components = function() { this.goldenlayout.registerComponent('comp1', function (container, state) { this.lister.init(container, state); }); }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Cree una variable en la parte exterior (normalmente la llamo self , pero cualquier cosa funciona) y utilícela en el interior.

 function ScreenCreator(config, container) { this.lister = new Lister(); this.goldenlayout = new GoldenLayout(config, container); this.create_components(); this.goldenlayout.init(); } ScreenCreator.prototype.create_components = function() { const self = this; this.goldenlayout.registerComponent('comp1', function (container, state) { self.lister.init(container, state); }); }

Alternativamente, puede usar una función de flecha, ya que no crean su propio this contexto.

 ScreenCreator.prototype.create_components = function() { this.goldenlayout.registerComponent('comp1', (container, state) => { this.lister.init(container, state); }); }

Si desea una forma extraña de hacerlo, que probablemente no debería usar a menos que los demás no funcionen, esto es lo siguiente: (agregando .bind(this) después de la función)

 ScreenCreator.prototype.create_components = function() { this.goldenlayout.registerComponent('comp1', (function (container, state) { this.lister.init(container, state); }).bind(this)); }
about 4 years ago · Juan Pablo Isaza Report

0

Podrías almacenar this in a variable como

 ScreenCreator.prototype.create_components = function() { let screenCreatorThis = this this.goldenlayout.registerComponent('comp1', function (container, state) { screenCreatorThis.lister.init(container, state); }); }
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!