Binding events to the object's prototype

Binding events to the object's prototype

Hi there,

I've just discovered something quite interesting: has it occurred to anyone that it's possible to bind events to the object's prototype and that you can fire those events from an instance method with the object as a target ?... have a look at this snippet: 

  1. /* @Class */
  2. var MyClass = function () {...}

  3. /* @Members */
  4. MyClass.prototype.methodThatTriggersAnEvent = function () {

  5.       // some code here
  6.       $(this).trigger( 'myevent', [...] );

  7. }

  8. /* @Events */
  9. $(MyClass.prototype).bind({

  10.       'myevent' : function (e, params) {
  11.             console.debug(e.target); // e.target = MyClass instance
  12.       }

  13. });

  14. var myobj = new MyClass();
  15. myobj.methodThatTriggersAnEvent(); // will trigger 'myevent' with myobj as e.target

I regard this as a really interesting and useful feature. Anyway, I just wanted to share it in case someone was looking to implement some Observer patter or Event Pool system using jQuery bind/unbind/trigger methods.