title: Javascript callbacks losing 'this'
body: <p>I have an issuer where I lose the <code>this</code> inside this object. The output of the following piece of JavaScript gives me <code>&quot;some-id&quot;</code> and then <code>undefined</code>. When I use <code>this</code> inside a callback function, the scope goes out of the object and it cannot use <code>this</code> any more. How can I get the callback to use 'this' or at least have access to the object?</p>
  <p>Since I will make multiple objects, I won't be able to create a 'static' like storage.</p>
  <p>Here is my test code that you can use to reproduce my problem. What I would like to have is <code>CheckBox.doSomething()</code> to return the value of <code>this.id</code> which should match <code>some-id</code> for this test case.</p>
  <pre><code>function CheckBox(input_id) {
      this.id = input_id;
      this.doSomething();
      $('#some-element').click(this.doSomething);
  }
  
  Checkbox.prototype.doSomething = function() {
      alert(this.input_id);
  }
  
  var some_box = new CheckBox('some-id');
  some_box.doSomething();
  $('#some-element').click();
  </code></pre>
  <p>I can't even get this to work as I want it to:</p>
  <pre><code>function CheckBox2(input_id) {
      this.id = input_id;
      alert(this.id);
  }
  
  CheckBox2.prototype.doSomething = function() {
      alert(this.input_id);
  }
  var some_box = new CheckBox2('some-id');
  some_box.doSomething();
  </code></pre>