Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
js, reference current object inside class (in the contex of the html element)

Is there some way to get current object inside class after running addEventListener other than using class instance name

See my code below (I keeped only relevant code)

class TicTacToe{

    constructor(){
        _self = this;
        this.events();   
        
    }

    events(){        
        this.cells.forEach(cell=>cell.addEventListener('click', this.selectCell))
    }

    selectCell(){
        console.log(this) //cell element
        console.log(_self) //undefined
        ticTacToe.otherMethod() // is this the only way?
    }
    otherMethod(){
 
    }

}

const ticTacToe = new TicTacToe();

Right now, 'this' in selectCell method reference cell element(not ticTacTo object) which is what I wanted but I can't access other methods in the context of that method. I was trying to set field _self=this in constructor but whenever I call it it's undefined(don't know why it's not working). Please let me know if calling instance name (ticTacToe.otherMethod()) is the only option

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda