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

114
Views
Acceda a la propiedad de un objeto desde la función jquery dentro del objeto (JavaScript/jQuery)

Tengo la siguiente clase:

 class Traffic { constructor(name) { this.name = name; } setEventListeners() { $("#btn").on('click', function() { $(this).text(name); } } }

Al hacer clic en el <button id="btn"></button> Esto arroja el error: el name is undefined . Esto tiene sentido para mí, ya que normalmente usaría this.name para acceder a las propiedades del objeto. Sin embargo, debido a que estoy dentro de jQuery, ahora this se refiere al objeto jQuery.

¿Cómo puedo acceder a la propiedad de nombre del objeto Traffic ?

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

0

Simplemente puede declarar una nueva variable fuera de la función donde this todavía apunta a la instancia de la clase.

 setEventListeners() { const name = this.name; $("#btn").on('click', function() { $(this).text(name); } }
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!