I have a JavaScript class that looks like this:
function SomeFunction()
{
this.doSomething(function()
{
this.doSomethingElse();
});
this.doSomethingElse = function()
{
}
}
This code throws an error because the scope of “this” inside the function that is passed into doSomething() is different that than the scope of “this” outside of that function.
I understand why this is, but what’s the best way to deal with this? This is what I end up doing:
function SomeFunction()
{
var thisObject = this;
this.doSomething(function()
{
thisObject.doSomethingElse();
});
this.doSomethingElse = function()
{
}
}
That works fine, but it just feels like a hack. Just wondering if someone has a better way.
This commenter had what I was looking for (although the other answers are good too):
@Jon Kruger See this, check out their article for call, too: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Function/Apply– Jonathon 44 mins ago