Function.js

Contains Function prototypes and utility functions .

License

MIT-style license.

Credits

  • Some functions are inspired by those found in prototype.js http://prototype.conio.net/ © 2005 Sam Stephenson sam [at] conio [dot] net, MIT-style license
Summary
Function.jsContains Function prototypes and utility functions .
FunctionA collection of The Function Object prototype methods.
Properties
createMain function to create closures.
passShortcut to create closures with arguments and bind.
attemptTries to execute the function, returns either the result of the function or false on error.
bindmethod to easily create closures with “this” altered.
bindAsEventListenercross browser method to pass event firer
delayDelays the execution of a function by a specified duration.
periodicalExecutes a function in the specified intervals of time

Function

A collection of The Function Object prototype methods.

Summary
Properties
createMain function to create closures.
passShortcut to create closures with arguments and bind.
attemptTries to execute the function, returns either the result of the function or false on error.
bindmethod to easily create closures with “this” altered.
bindAsEventListenercross browser method to pass event firer
delayDelays the execution of a function by a specified duration.
periodicalExecutes a function in the specified intervals of time

Properties

create

Main function to create closures.

Returns

a function.

Arguments

optionsAn Options object.

Options

bindThe object that the “this” of the function will refer to.  Default is the current function.
eventIf set to true, the function will act as an event listener and receive an event as first argument.  If set to a class name, the function will receive a new instance of this class (with the event passed as argument’s constructor) as first argument.  Default is false.
argumentsA single argument or array of arguments that will be passed to the function when called.

If both the event and arguments options are set, the event is passed as first argument and the arguments array will follow.

Default is no custom arguments, the function will receive the standard arguments when called.

delayNumeric value: if set, the returned function will delay the actual execution by this amount of milliseconds and return a timer handle when called.  Default is no delay.
periodicalNumeric value: if set, the returned function will periodically perform the actual execution with this specified interval and return a timer handle when called.  Default is no periodical execution.
attemptIf set to true, the returned function will try to execute and return either the results or false on error.  Default is false.

pass

Shortcut to create closures with arguments and bind.

Returns

a function.

Arguments

argsthe arguments passed. must be an array if arguments > 1
bindoptional, the object that the “this” of the function will refer to.

Example

myFunction.pass([arg1, arg2], myElement);

attempt

Tries to execute the function, returns either the result of the function or false on error.

Arguments

argsthe arguments passed. must be an array if arguments > 1
bindoptional, the object that the “this” of the function will refer to.

Example

myFunction.attempt([arg1, arg2], myElement);

bind

method to easily create closures with “this” altered.

Arguments

bindoptional, the object that the “this” of the function will refer to.
argsoptional, the arguments passed. must be an array if arguments > 1

Returns

a function.

Example

function myFunction(){
   this.setStyle('color', 'red');
   // note that 'this' here refers to myFunction, not an element
   // we'll need to bind this function to the element we want to alter
};
var myBoundFunction = myFunction.bind(myElement);
myBoundFunction(); // this will make the element myElement red.

bindAsEventListener

cross browser method to pass event firer

Arguments

bindoptional, the object that the “this” of the function will refer to.
argsoptional, the arguments passed. must be an array if arguments > 1

Returns

a function with the parameter bind as its “this” and as a pre-passed argument event or window.event, depending on the browser.

Example

function myFunction(event){
   alert(event.clientx) //returns the coordinates of the mouse..
};
myElement.onclick = myFunction.bindAsEventListener(myElement);

delay

Delays the execution of a function by a specified duration.

Arguments

delaythe duration to wait in milliseconds.
bindoptional, the object that the “this” of the function will refer to.
argsoptional, the arguments passed. must be an array if arguments > 1

Example

myFunction.delay(50, myElement) //wait 50 milliseconds, then call myFunction and bind myElement to it
(function(){alert('one second later...')}).delay(1000); //wait a second and alert

periodical

Executes a function in the specified intervals of time

Arguments

intervalthe duration of the intervals between executions.
bindoptional, the object that the “this” of the function will refer to.
argsoptional, the arguments passed. must be an array if arguments > 1

Documentation by Aaron Newton & Mootools Developers, generated by NaturalDocs and GeSHi