Rob Kinyon - Function.bind-0.02

Documentation | Source

NAME

Function.bindAsEventListener

DESCRIPTION

This provides a new method to the core class Function. The method, called bindAsEventListener(), is used to do bind methods to event handlers.

DEPENDENCIES

This requires JSAN to be installed.

CORE CLASS EXTENSIONS

These are extensions to core classes provided by JavaScript.

Function.prototype.bindAsEventListener( object )

The bindAsEventListener() method is added to the core Function class, providing the ability to create a closure over a method that can be bound to an event handler.

  var obj = new SomeClass();

  var closure = obj.someMethod.bindAsEventListener( this );

  element.onClick = closure;

CAVEATS

Modifying the prototype of core Javascript classes should be avoided, if possible. By doing this, you are modifying ALL objects of this class, regardless of when they were instantiated or by whom, For some classes, such as Number and String, this includes primitives. This can lead to surprising effects and action-at-a-distance.

You have been warned.

SUPPORT

Currently, there is no mailing list or IRC channel. Please send bug reports and patches to the author.

AUTHOR

Rob Kinyon (rob.kinyon@iinteractive.com)

Originally written by Sam Stephenson (sam@conio.net)

My time is generously donated by Infinity Interactive, Inc. http://www.iinteractive.com

/*

=head1 NAME

Function.bindAsEventListener

=head1 DESCRIPTION

This provides a new method to the core class Function. The method, called bindAsEventListener(), is used to do bind methods to event handlers.

=head1 DEPENDENCIES

This requires JSAN to be installed.

=cut

*/

/*

=head1 CORE CLASS EXTENSIONS

These are extensions to core classes provided by JavaScript.

=cut

*/

/*

=head2 Function.prototype.bindAsEventListener( object )

The C<bindAsEventListener()> method is added to the core Function class, providing the ability to create a closure over a method that can be bound to an event handler.

  var obj = new SomeClass();

  var closure = obj.someMethod.bindAsEventListener( this );

  element.onClick = closure;

=cut

*/

if ( ! Function.prototype.bindAsEventListener ) {
    Function.prototype.bindAsEventListener = function(object) {
        var __method = this;
        return function(event) {
            __method.call(object, event || window.event);
        };
    };
}

/*

=head1 CAVEATS

Modifying the prototype of core Javascript classes should be avoided, if possible. By doing this, you are modifying B<ALL> objects of this class, regardless of when they were instantiated or by whom, For some classes, such as Number and String, this includes primitives. This can lead to surprising effects and action-at-a-distance.

You have been warned.

=head1 SUPPORT

Currently, there is no mailing list or IRC channel. Please send bug reports and patches to the author.

=head1 AUTHOR

Rob Kinyon (rob.kinyon@iinteractive.com)

Originally written by Sam Stephenson (sam@conio.net)

My time is generously donated by Infinity Interactive, Inc. L<http://www.iinteractive.com>

=cut

*/