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

239
Views
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
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!