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

110
Views
¿Cómo se accede al contexto `this` de una instancia de clase desde un controlador de eventos sin usar `bind`?

Tengo una clase y confío en que la palabra clave "esto" mantenga su contexto de clase. Sin embargo, cuando uso un controlador de eventos dentro de la clase, "este" contexto cambia al evento. Sé que puedo eventFunction(){}.bind(this) , pero también necesito el contexto 'this' del evento.

Pensé en la siguiente solución "Asignar la clase 'esto' como una variable predeterminada"; parece funcionar, pero no estoy seguro de que este sea el enfoque correcto:

 class A_Test { constructor(a, b) { this.selector = '.foam_types'; } bind_foam_type_radios() { $(this.selector).on({ mouseenter: this.showFoamTypeExample, mouseleave: this.hideFoamTypeExample, click: this.selectFoamType, }, '.foam_type_option'); } showFoamTypeExample(e, el = this) // <-- el is being assigned the Class' "THIS" { console.log(this); // Event's "this" console.log(el); // Class' "this" } } var TheTest = A_Test(); TheTest.bind_foam_type_radios();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En el caso de que se utilice un método de instancia (propio o prototipo) como controlador de eventos sin this bind explícita, se debe implementar una función de controlador como flecha para acceder directamente a this contexto (adjunto) del tiempo de creación de instancias.

 class A_Test { constructor(a, b) { this.selector = '.foam-types'; } registerFoamTypeRadios() { $(this.selector).on('click', '[name="foam-type"]', this.selectFoamType); // mouseenter: this.showFoamTypeExample, // mouseleave: this.hideFoamTypeExample, } selectFoamType = (evt) => { const elm = evt.currentTarget; const srcElm = evt.target; console.log({ this: this, className: this.constructor.name, elm, srcElm }); } showFoamTypeExample = (evt) => {} hideFoamTypeExample = (evt) => {} } var theTest = new A_Test(); theTest.registerFoamTypeRadios();
 * { margin: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <fieldset class="foam-types"> <legend>Foam Types</legend> <label class="foam-type-option"> <input type="radio" name="foam-type" value="expanding"/> <span>Expanding Foam</span> </label> <label class="foam-type-option"> <input type="radio" name="foam-type" value="shaving"/> <span>Shaving Foam</span> </label> </fieldset>

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!