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.
thiswithcallandapply, but that doesn't always fit what you're doing well. Another option is passingthisas a parameter, but that feels like even more of a hack.call, too: developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/…