Delegates in Javascript

I've recently been doing javascript coding again. Being the object bigot that I am, everything that interacts with a DOM element gets wrapped in an object that becomes responsible for that element's behavior. Well, then i tried to move all the event handler code from the procedural side to the object side and things broke, and hard.

At first I was confused why it wouldn't call my this._InternalMethod inside my event handler. Then I remembered that i've been spoiled by the CLR and that I was dealing with plain old function pointers, not delegates.

While the Atlas framework provides delegate functionality (along with a lot of other useful things), this was not for a .NET 2.0 project and I didn't want to graft the Atlas clientside onto it as a dependency. But knowing that Atlas does delegates, i knew it was possible.. but how?

I found the answer in this article which basically uses closures in javascript to allow the persistent of the object context in event handlers.

So basically to create an event handler that maintains its object context do this:

function MyObject = function(name)
{
  this._name = name;
  var _this = this;

  this.MyEventHandler = function()
  {
    alert("My name is "+_this._name;
  }
}

Great. Now I can avoid all procedural code and just have my object subscribe themselves to element and document events and handle them in their own context