3 Contains Function prototypes, utility functions and Chain.
9 Valerio Proietti, <http://mad4milk.net>
15 - Some functions are inspired by those found in prototype.js <http://prototype.conio.net/> (c) 2005 Sam Stephenson sam [at] conio [dot] net, MIT-style license
20 A collection of The Function Object prototype methods.
27 Shortcut to create closures with arguments and bind.
33 args - the arguments to pass to that function (array or single variable)
34 bind - optional, the object that the "this" of the function will refer to.
37 >myFunction.pass([arg1, arg2], myElement);
40 pass: function(args, bind){
42 if ($type(args) != 'array') args = [args];
44 return fn.apply(bind || fn._proto_ || fn, args);
50 method to easily create closures with "this" altered.
53 bind - optional, the object that the "this" of the function will refer to.
59 >function myFunction(){
60 > this.setStyle('color', 'red');
61 > // note that 'this' here refers to myFunction, not an element
62 > // we'll need to bind this function to the element we want to alter
64 >var myBoundFunction = myFunction.bind(myElement);
65 >myBoundFunction(); // this will make the element myElement red.
71 return fn.apply(bind, arguments);
76 Property: bindAsEventListener
77 cross browser method to pass event firer
80 bind - optional, the object that the "this" of the function will refer to.
83 a function with the parameter bind as its "this" and as a pre-passed argument event or window.event, depending on the browser.
86 >function myFunction(event){
87 > alert(event.clientx) //returns the coordinates of the mouse..
89 >myElement.onclick = myFunction.bindAsEventListener(myElement);
92 bindAsEventListener: function(bind){
94 return function(event){
95 fn.call(bind, event || window.event);
102 Delays the execution of a function by a specified duration.
105 ms - the duration to wait in milliseconds
106 bind - optional, the object that the "this" of the function will refer to.
109 >myFunction.delay(50, myElement) //wait 50 milliseconds, then call myFunction and bind myElement to it
110 >(function(){alert('one second later...')}).delay(1000); //wait a second and alert
113 delay: function(ms, bind){
114 return setTimeout(this.bind(bind || this._proto_ || this), ms);
119 Executes a function in the specified intervals of time
122 ms - the duration of the intervals between executions.
123 bind - optional, the object that the "this" of the function will refer to.
126 periodical: function(ms, bind){
127 return setInterval(this.bind(bind || this._proto_ || this), ms);
132 /* Section: Utility Functions */
136 clears a timeout or an Interval.
142 timer - the setInterval or setTimeout to clear.
145 >var myTimer = myFunction.delay(5000); //wait 5 seconds and execute my function.
146 >myTimer = $clear(myTimer); //nevermind
149 <Function.delay>, <Function.periodical>
152 function $clear(timer){
154 clearInterval(timer);
160 Returns the type of object that matches the element passed in.
163 obj - the object to inspect.
166 >var myString = 'hello';
167 >$type(myString); //returns "string"
170 'function' - if obj is a function
171 'textnode' - if obj is a node but not an element
172 'element' - if obj is a DOM element
173 'array' - if obj is an array
174 'object' - if obj is an object
175 'string' - if obj is a string
176 'number' - if obj is a number
177 false - (boolean) if the object is not defined or none of the above, or if it's an empty string.
181 if (!obj) return false;
183 if (obj instanceof Function) type = 'function';
184 else if (obj.nodeName){
185 if (obj.nodeType == 3 && !/\S/.test(obj.nodeValue)) type = 'textnode';
186 else if (obj.nodeType == 1) type = 'element';
188 else if (obj instanceof Array) type = 'array';
189 else if (typeof obj == 'object') type = 'object';
190 else if (typeof obj == 'string') type = 'string';
191 else if (typeof obj == 'number' && isFinite(obj)) type = 'number';
197 var Chain = new Class({
201 adds a function to the Chain instance stack.
204 fn - the function to append.
207 the instance of the <Chain> class.
210 >var myChain = new Chain();
211 >myChain.chain(myFunction).chain(myFunction2);
215 this.chains = this.chains || [];
216 this.chains.push(fn);
222 Executes the first function of the Chain instance stack, then removes it. The first function will then become the second.
225 >myChain.callChain(); //executes myFunction
226 >myChain.callChain(); //executes myFunction2
229 callChain: function(){
230 if (this.chains && this.chains.length) this.chains.splice(0, 1)[0].delay(10, this);
235 Clears the stack of a Chain instance.
238 clearChain: function(){