Function.js

Contains Function prototypes, utility functions and Chain.

Dependencies

Moo.js

Author

Valerio Proietti, http://mad4milk.net

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.js Contains Function prototypes, utility functions and Chain.
Function A collection of The Function Object prototype methods.
Properties
pass Shortcut to create closures with arguments and bind.
bind method to easily create closures with “this” altered.
bindAsEventListener cross browser method to pass event firer
delay Delays the execution of a function by a specified duration.
periodical Executes a function in the specified intervals of time
Utility Functions
Functions
$clear clears a timeout or an Interval.
$type Returns the type of object that matches the element passed in.
Chain
Properties
chain adds a function to the Chain instance stack.
callChain Executes the first function of the Chain instance stack, then removes it.
clearChain Clears the stack of a Chain instance.

Function

A collection of The Function Object prototype methods.

Summary
Properties
pass Shortcut to create closures with arguments and bind.
bind method to easily create closures with “this” altered.
bindAsEventListener cross browser method to pass event firer
delay Delays the execution of a function by a specified duration.
periodical Executes a function in the specified intervals of time

Properties

pass

Shortcut to create closures with arguments and bind.

Returns

a function.

Arguments

args the arguments to pass to that function (array or single variable)
bind optional, the object that the “this” of the function will refer to.

Example

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

bind

method to easily create closures with “this” altered.

Arguments

bind optional, the object that the “this” of the function will refer to.

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

bind optional, the object that the “this” of the function will refer to.

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

ms the duration to wait in milliseconds
bind optional, the object that the “this” of the function will refer to.

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

ms the duration of the intervals between executions.
bind optional, the object that the “this” of the function will refer to.

Utility Functions

Summary
Functions
$clear clears a timeout or an Interval.
$type Returns the type of object that matches the element passed in.

Functions

$clear

function $clear( timer )

clears a timeout or an Interval.

Returns

null

Arguments

timer the setInterval or setTimeout to clear.

Example

var myTimer = myFunction.delay(5000); //wait 5 seconds and execute my function.
myTimer = $clear(myTimer); //nevermind

See also

Function.delay, Function.periodical

$type

function $type( obj )

Returns the type of object that matches the element passed in.

Arguments

obj the object to inspect.

Example

var myString = 'hello';
$type(myString); //returns "string"

Returns

’function’ if obj is a function
’textnode’ if obj is a node but not an element
’element’ if obj is a DOM element
’array’ if obj is an array
’object’ if obj is an object
’string’ if obj is a string
’number’ if obj is a number
false (boolean) if the object is not defined or none of the above, or if it’s an empty string.

Chain

Summary
Properties
chain adds a function to the Chain instance stack.
callChain Executes the first function of the Chain instance stack, then removes it.
clearChain Clears the stack of a Chain instance.

Properties

chain

adds a function to the Chain instance stack.

Arguments

fn the function to append.

Returns

the instance of the Chain class.

Example

var myChain = new Chain();
myChain.chain(myFunction).chain(myFunction2);

callChain

Executes the first function of the Chain instance stack, then removes it.  The first function will then become the second.

Example

myChain.callChain(); //executes myFunction
myChain.callChain(); //executes myFunction2

clearChain

Clears the stack of a Chain instance.

function $clear( timer )
clears a timeout or an Interval.
function $type( obj )
Returns the type of object that matches the element passed in.
My Object Oriented javascript.
Delays the execution of a function by a specified duration.
Executes a function in the specified intervals of time