Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda