Scope of "this" in JavaScript
<p>I have a JavaScript class that looks like this:</p>

<pre><code>function SomeFunction()
{
    this.doSomething(function()
    {
        this.doSomethingElse();
    });

    this.doSomethingElse = function()
    {

    }
}
</code></pre>

<p>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.  </p>

<p>I understand why this is, but what's the best way to deal with this?  This is what I end up doing:</p>

<pre><code>function SomeFunction()
{
    var thisObject = this;

    this.doSomething(function()
    {
        thisObject.doSomethingElse();
    });

    this.doSomethingElse = function()
    {

    }
}
</code></pre>

<p>That works fine, but it just feels like a hack.  Just wondering if someone has a better way.</p>
