instance method Function#bindAsEventListener

View source on GitHub →

Function#bindAsEventListener(context[, args...]) → Function
  • context (Object) – The object to bind to.
  • args (?) – Optional arguments to curry after the event argument.

An event-specific variant of Function#bind which ensures the function will recieve the current event object as the first argument when executing.

It is not necessary to use bindAsEventListener for all bound event handlers; Function#bind works well for the vast majority of cases. bindAsEventListener is only needed when:

  • Using old-style DOM0 handlers rather than handlers hooked up via Event.observe, because bindAsEventListener gets the event object from the right place (even on MSIE). (If you're using Event.observe, that's already handled.)
  • You want to bind an event handler and curry additional arguments but have those arguments appear after, rather than before, the event object. This mostly happens if the number of arguments will vary, and so you want to know the event object is the first argument.
var ContentUpdater = Class.create({
  initialize: function(initialData) { = Object.extend({}, initialData);
  // On an event, update the content in the elements whose
  // IDs are passed as arguments from our data
  updateTheseHandler: function(event) {
    var argIndex, id, element;
    for (argIndex = 1; argIndex < arguments.length; ++argIndex) {
      id = arguments[argIndex];
      element = $(id);
      if (element) {
var cu = new ContentUpdater({
  dispName: 'Joe Bloggs',
  dispTitle: 'Manager <provisional>',
  dispAge: 47
// Using bindAsEventListener because of the variable arg lists:
  cu.updateTheseHandler.bindAsEventListener(cu, 'dispName')
  cu.updateTheseHandler.bindAsEventListener(cu, 'dispName', 'dispTitle', 'dispAge')