3 Contains Function prototypes and utility functions .
9 - 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
14 A collection of The Function Object prototype methods.
21 Main function to create closures.
27 options - An Options object.
30 bind - The object that the "this" of the function will refer to. Default is the current function.
31 event - If set to true, the function will act as an event listener and receive an event as first argument.
32 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.
34 arguments - A single argument or array of arguments that will be passed to the function when called.
36 If both the event and arguments options are set, the event is passed as first argument and the arguments array will follow.
38 Default is no custom arguments, the function will receive the standard arguments when called.
40 delay - Numeric value: if set, the returned function will delay the actual execution by this amount of milliseconds and return a timer handle when called.
42 periodical - Numeric value: if set, the returned function will periodically perform the actual execution with this specified interval and return a timer handle when called.
43 Default is no periodical execution.
44 attempt - If set to true, the returned function will try to execute and return either the results or false on error. Default is false.
47 create: function(options){
57 if ($chk(options.arguments) && $type(options.arguments) != 'array') options.arguments = [options.arguments];
58 return function(event){
61 event = event || window.event;
62 args = [(options.event === true) ? event : new options.event(event)];
63 if (options.arguments) args.extend(options.arguments);
65 else args = options.arguments || arguments;
66 var returns = function(){
67 return fn.apply($pick(options.bind, fn), args);
69 if (options.delay) return setTimeout(returns, options.delay);
70 if (options.periodical) return setInterval(returns, options.periodical);
71 if (options.attempt) try {return returns();} catch(err){return false;};
78 Shortcut to create closures with arguments and bind.
84 args - the arguments passed. must be an array if arguments > 1
85 bind - optional, the object that the "this" of the function will refer to.
88 >myFunction.pass([arg1, arg2], myElement);
91 pass: function(args, bind){
92 return this.create({'arguments': args, 'bind': bind});
97 Tries to execute the function, returns either the result of the function or false on error.
100 args - the arguments passed. must be an array if arguments > 1
101 bind - optional, the object that the "this" of the function will refer to.
104 >myFunction.attempt([arg1, arg2], myElement);
107 attempt: function(args, bind){
108 return this.create({'arguments': args, 'bind': bind, 'attempt': true})();
113 method to easily create closures with "this" altered.
116 bind - optional, the object that the "this" of the function will refer to.
117 args - optional, the arguments passed. must be an array if arguments > 1
123 >function myFunction(){
124 > this.setStyle('color', 'red');
125 > // note that 'this' here refers to myFunction, not an element
126 > // we'll need to bind this function to the element we want to alter
128 >var myBoundFunction = myFunction.bind(myElement);
129 >myBoundFunction(); // this will make the element myElement red.
132 bind: function(bind, args){
133 return this.create({'bind': bind, 'arguments': args});
137 Property: bindAsEventListener
138 cross browser method to pass event firer
141 bind - optional, the object that the "this" of the function will refer to.
142 args - optional, the arguments passed. must be an array if arguments > 1
145 a function with the parameter bind as its "this" and as a pre-passed argument event or window.event, depending on the browser.
148 >function myFunction(event){
149 > alert(event.clientx) //returns the coordinates of the mouse..
151 >myElement.onclick = myFunction.bindAsEventListener(myElement);
154 bindAsEventListener: function(bind, args){
155 return this.create({'bind': bind, 'event': true, 'arguments': args});
160 Delays the execution of a function by a specified duration.
163 delay - the duration to wait in milliseconds.
164 bind - optional, the object that the "this" of the function will refer to.
165 args - optional, the arguments passed. must be an array if arguments > 1
168 >myFunction.delay(50, myElement) //wait 50 milliseconds, then call myFunction and bind myElement to it
169 >(function(){alert('one second later...')}).delay(1000); //wait a second and alert
172 delay: function(delay, bind, args){
173 return this.create({'delay': delay, 'bind': bind, 'arguments': args})();
178 Executes a function in the specified intervals of time
181 interval - the duration of the intervals between executions.
182 bind - optional, the object that the "this" of the function will refer to.
183 args - optional, the arguments passed. must be an array if arguments > 1
186 periodical: function(interval, bind, args){
187 return this.create({'periodical': interval, 'bind': bind, 'arguments': args})();