0

I'm simply trying to access a class method from within a function within another class method so:

class test
{
 show()
 {
  setTimeout(function()
  {
    this.showInside()
  },0)

 }

 showInside()
 {
    alert("WORKING")
 }



}

var test2 = new test();
test2.show()

I'm obviously doing something wrong, and clearly I can't use this.showInside() within that function, but I can't figure out what I need to do...

Any help?

TheBounder
  • 39
  • 4

1 Answers1

0

this depends on the context. Inside setTimeout, this doesn't refer to the instance. Here's a working example :

class test
{
  constructor(){};

 show(){
  setTimeout((function(){ this.showInside() }).bind(this),0)
 }

 showInside(){
    alert("WORKING");
 }

}

var test2 = new test();
test2.show();
madjaoue
  • 5,104
  • 2
  • 19
  • 31