Class methods as event handlers in javascript?
<p>Is there a best-practice or common way in Javascript to have class members as event handlers?</p>

<p>Consider the following simple example:</p>

<pre><code>&lt;head&gt;
    &lt;script language="javascript" type="text/javascript"&gt;

        ClickCounter = function(buttonId) {
            this._clickCount = 0;
            document.getElementById(buttonId).onclick = this.buttonClicked;
        }

        ClickCounter.prototype = {
            buttonClicked: function() {
                this._clickCount++;
                alert('the button was clicked ' + this._clickCount + ' times');
            }
        }

    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;input type="button" id="btn1" value="Click me" /&gt;
    &lt;script language="javascript" type="text/javascript"&gt;
        var btn1counter = new ClickCounter('btn1');
    &lt;/script&gt;
&lt;/body&gt;
</code></pre>

<p>The event handler buttonClicked get called, but the _clickCount member is inaccessible, or <em>this</em> points to some other object.</p>

<p>Any good tips/articles/resources about this kind of problems?</p>

<p>Best regards, JacobE</p>
