Rob Kinyon - Function.bind-0.02

Documentation | Source

NAME

Function.bind

DESCRIPTION

This provides a new method to the core class Function. The method, called bind(), is used to create closures for use with window.setTimeout().

DEPENDENCIES

This requires JSAN and Upgrade to be installed.

CORE CLASS EXTENSIONS

These are extensions to core classes provided by JavaScript.

Function.prototype.bind( object )

The bind() method is added to the core Function class, providing the ability to create a closure over a method useful for passing to setTimeout().

  var obj = new SomeClass();

  var closure = obj.someMethod.bind( this );
  window.setTimeout( closure, 10 );

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.bind

=head1 DESCRIPTION

This provides a new method to the core class Function. The method, called bind(), is used to create closures for use with window.setTimeout().

=head1 DEPENDENCIES

This requires JSAN and Upgrade to be installed.

=cut

*/

try {
    JSAN.use( 'Upgrade.Function.apply' );
} catch (e) {
    throw "Function.bind requires JSAN to be loaded";
}

/*

=head1 CORE CLASS EXTENSIONS

These are extensions to core classes provided by JavaScript.

=cut

*/

/*

=head2 Function.prototype.bind( object )

The C<bind()> method is added to the core Function class, providing the ability to create a closure over a method useful for passing to setTimeout().

  var obj = new SomeClass();

  var closure = obj.someMethod.bind( this );
  window.setTimeout( closure, 10 );

=cut

*/

if ( ! Function.prototype.bind ) {
    Function.prototype.bind = function( object ) {
        var __method = this;
        return function() {
            __method.apply( object, arguments );
        };
    };
}

/*

=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

*/