1

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

0 Answers0