1 Native: Function {#Function}
2 ============================
12 Function Method: create {#Function:create}
13 ------------------------------------------
15 Base function for creating functional closures which is used by all other Function prototypes.
19 var createdFunction = myFunction.create([options]);
23 1. [options] - (*object*, optional) The options from which the function will be created. If options is not provided, then creates a copy of the function.
25 #### Options: {#Function:create:options}
27 * bind - (*object*: defaults to this function) The object that the "this" of the function will refer to.
28 * event - (*mixed*: defaults to false) If set to true, the function will act as an event listener and receive an event as its 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.
29 * arguments - (*mixed*: defaults to standard arguments) A single argument or an array of arguments that will be passed as arguments to the function. If both the event and arguments options are set, the event is passed as first argument and the arguments array will follow.
30 * delay - (*number*: defaults to no delay) If set, the returned function will delay the actual execution by this amount of milliseconds and return a timer handle when called.
31 * periodical - (*number*: defaults to no periodical execution) If set, the returned function will periodically perform the actual execution with this specified interval and return a timer handle when called.
32 * attempt - (*boolean*: false) If set to true, the returned function will try to execute and return either the results or null on error.
36 * (*function*) The function that was created as a result of the options passed in.
40 var myFunction = function(){
41 alert("I'm a function. :D");
44 var mySimpleFunction = myFunction.create(); //Just a simple copy.
46 var myAdvancedFunction = myFunction.create({ //When called, this function will attempt.
55 Function Method: pass {#Function:pass}
56 --------------------------------------
58 Returns a closure with arguments and bind.
62 var newFunction = myFunction.pass([args[, bind]]);
66 1. args - (*mixed*, optional) The arguments to pass to the function (must be an array if passing more than one argument).
67 2. bind - (*object*, optional) The object that the "this" of the function will refer to.
71 * (*function*) The function whose arguments are passed when called.
75 var myFunction = function(){
76 var result = 'Passed: ';
77 for (var i = 0, l = arguments.length; i < l; i++){
78 result += (arguments[i] + ' ');
82 var myHello = myFunction.pass('hello');
83 var myItems = myFunction.pass(['peach', 'apple', 'orange']);
85 //Later in the code, the functions can be executed:
86 alert(myHello()); //Passes "hello" to myFunction.
87 alert(myItems()); //Passes the array of items to myFunction.
91 Function Method: attempt {#Function:attempt}
92 --------------------------------------------
94 Tries to execute the function.
98 var result = myFunction.attempt([args[, bind]]);
102 1. args - (*mixed*, optional) The arguments to pass to the function (must be an array if passing more than one argument).
103 2. bind - (*object*, optional) The object that the "this" of the function will refer to.
107 * (*mixed*) The function's return value or `null` if an exception is thrown.
115 var myFunction = function(){
116 for (var i = 0; i < arguments.length; i++){
117 if(!this[arguments[i]]) throw('doh!');
120 var result = myFunction.attempt(['pig', 'cow'], myObject); //result = null
124 Function Method: bind {#Function:bind}
125 --------------------------------------
127 Changes the scope of `this` within the target function to refer to the bind parameter.
131 myFunction.bind([bind[, args[, evt]]]);
135 1. bind - (*object*, optional) The object that the "this" of the function will refer to.
136 2. args - (*mixed*, optional) The arguments to pass to the function (must be an array if passing more than one argument).
140 * (*function*) The bound function.
144 function myFunction(){
145 //Note that 'this' here refers to window, not an element.
146 //The function must be bound to the element we want to manipulate.
147 this.setStyle('color', 'red');
149 var myBoundFunction = myFunction.bind(myElement);
150 myBoundFunction(); //This will make myElement's text red.
154 Function Method: bindWithEvent {#Function:bindWithEvent}
155 --------------------------------------------------------
157 Changes the scope of `this` within the target function to refer to the bind parameter. It also makes "space" for an event.
158 This allows the function to be used in conjunction with [Element:addEvent][] and arguments.
162 myFunction.bindWithEvent([bind[, args[, evt]]]);
166 1. bind - (*object*, optional) The object that the "this" of the function will refer to.
167 2. args - (*mixed*, optional) The arguments to pass to the function (must be an array if passing more than one argument).
171 * (*function*) The bound function.
175 var Logger = new Class({
177 console.log.apply(null, arguments);
181 var Log = new Logger();
183 $('myElement').addEvent('click', function(event, offset){
184 offset += event.client.x;
185 this.log('clicked; moving to:', offset); // this refers to myClass
186 event.target.setStyle('top', offset);
188 }).bindWithEvent(Log, 100));
191 Function Method: delay {#Function:delay}
192 ----------------------------------------
194 Delays the execution of a function by a specified duration.
198 var timeoutID = myFunction.delay(delay[, bind[, args]]);
202 1. delay - (*number*) The duration to wait (in milliseconds).
203 2. bind - (*object*, optional) The object that the "this" of the function will refer to.
204 3. args - (*mixed*, optional) The arguments passed (must be an array if the arguments are greater than one).
208 * (*number*) The JavaScript timeout id (for clearing delays).
212 var myFunction = function(){ alert('moo! Element id is: ' + this.id); };
213 //Wait 50 milliseconds, then call myFunction and bind myElement to it.
214 myFunction.delay(50, myElement); //Alerts: 'moo! Element id is: ... '
216 //An anonymous function which waits a second and then alerts.
217 (function(){ alert('one second later...'); }).delay(1000);
222 - [$clear][], [MDC setTimeout][]
226 Function Method: periodical {#Function:periodical}
227 --------------------------------------------------
229 Executes a function in the specified intervals of time. Periodic execution can be stopped using the [$clear][] function.
233 var intervalID = myFunction.periodical(period[, bind[, args]]);
237 1. period - (*number*) The duration of the intervals between executions.
238 2. bind - (*object*, optional) The object that the "this" of the function will refer to.
239 3. args - (*mixed*, optional) The arguments passed (must be an array if the arguments are greater than one).
243 * (*number*) The Interval id (for clearing a periodical).
247 var Site = { counter: 0 };
248 var addCount = function(){ this.counter++; };
249 addCount.periodical(1000, Site); //Will add the number of seconds at the Site.
254 - [$clear][], [MDC setInterval][]
258 Function Method: run {#Function:run}
259 ------------------------------------
261 Runs the Function with specified arguments and binding. The same as apply but reversed and with support for a single argument.
265 var myFunctionResult = myFunction.run(args[, bind]);
269 1. args - (*mixed*) An argument, or array of arguments to run the function with.
270 2. bind - (*object*, optional) The object that the "this" of the function will refer to.
274 * (*mixed*) This Function's return value.
280 var myFn = function(a, b, c){
283 var myArgs = [1,2,3];
284 myFn.run(myArgs); //Returns: 6
287 #### Run With Binding:
289 var myFn = function(a, b, c) {
290 return a + b + c + this;
292 var myArgs = [1,2,3];
293 myFn.run(myArgs, 6); //Returns: 12
297 [options]: #Function:create:options
298 [Element:addEvent]: /core/Element/Element.Event/#Element:addEvent
299 [$clear]: /core/Core/Core/#clear
300 [MDC Function]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function
301 [MDC setInterval]: http://developer.mozilla.org/en/docs/DOM:window.setInterval
302 [MDC setTimeout]: http://developer.mozilla.org/en/docs/DOM:window.setTimeout