"this" inside setTimeout

Hi everyone! It’s been a long since I posted a new thread… ahh good old kirupa forum :slight_smile:

Anyway… I have a class which I pass a config object when instantiated. in this object I pass a scope param to be able to call functions and vars from the main class. Like this


trace(this)// gives me [object NameOfTheDocumentClass]

var myObj = new MyClass({scope:this});

… so from inside the MyClass I do this to call myFunction on the main document class:


config.scope.myFunction();

Problem is, now I want to instantiate the class inside a setTimeout and that doesn’t work, because “this” refers to something else… and not the document class.



myTimeout = setTimeout(function(){
  trace(this)// now gives me [object global] instead of the document class
  var myObj = new MyClass({scope:this});
},1000);

I found a solution to this problem, but I’d like to know how could a reference the main class in this situation… and why this becomes [object global] ???

Regards and happy coding :wink: