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
function myFunction(e, add){
176
//Note that 'this' here refers to window, not an element.
177
//We'll need to bind this function to the element we want to alter.
178
this.setStyle('top', e.client.x + add);
180
$(myElement).addEvent('click', myFunction.bindWithEvent(myElement, 100));
181
//When clicked, the element will move to the position of the mouse + 100.
185
Function Method: delay {#Function:delay}
186
----------------------------------------
188
Delays the execution of a function by a specified duration.
192
var timeoutID = myFunction.delay(delay[, bind[, args]]);
196
1. delay - (*number*) The duration to wait (in milliseconds).
197
2. bind - (*object*, optional) The object that the "this" of the function will refer to.
198
3. args - (*mixed*, optional) The arguments passed (must be an array if the arguments are greater than one).
202
* (*number*) The JavaScript timeout id (for clearing delays).
206
var myFunction = function(){ alert('moo! Element id is: ' + this.id); };
207
//Wait 50 milliseconds, then call myFunction and bind myElement to it.
208
myFunction.delay(50, myElement); //Alerts: 'moo! Element id is: ... '
210
//An anonymous function which waits a second and then alerts.
211
(function(){ alert('one second later...'); }).delay(1000);
216
- [$clear][], [MDC setTimeout][]
220
Function Method: periodical {#Function:periodical}
221
--------------------------------------------------
223
Executes a function in the specified intervals of time. Periodic execution can be stopped using the [$clear][] function.
227
var intervalID = myFunction.periodical(period[, bind[, args]]);
231
1. period - (*number*) The duration of the intervals between executions.
232
2. bind - (*object*, optional) The object that the "this" of the function will refer to.
233
3. args - (*mixed*, optional) The arguments passed (must be an array if the arguments are greater than one).
237
* (*number*) The Interval id (for clearing a periodical).
241
var Site = { counter: 0 };
242
var addCount = function(){ this.counter++; };
243
addCount.periodical(1000, Site); //Will add the number of seconds at the Site.
248
- [$clear][], [MDC setInterval][]
252
Function Method: run {#Function:run}
253
------------------------------------
255
Runs the Function with specified arguments and binding. The same as apply but reversed and with support for a single argument.
259
var myFunctionResult = myFunction.run(args[, bind]);
263
1. args - (*mixed*) An argument, or array of arguments to run the function with.
264
2. bind - (*object*, optional) The object that the "this" of the function will refer to.
268
* (*mixed*) This Function's return value.
274
var myFn = function(a, b, c){
277
var myArgs = [1,2,3];
278
myFn.run(myArgs); //Returns: 6
281
#### Run With Binding:
283
var myFn = function(a, b, c) {
284
return a + b + c + this;
286
var myArgs = [1,2,3];
287
myFn.run(myArgs, 6); //Returns: 12
291
[options]: #Function:create:options
292
[Element:addEvent]: /Element/Element.Event/#Element:addEvent
293
[$clear]: /Core/Core/#clear
294
[MDC Function]: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function
295
[MDC setInterval]: http://developer.mozilla.org/en/docs/DOM:window.setInterval
296
[MDC setTimeout]: http://developer.mozilla.org/en/docs/DOM:window.setTimeout
b'\\ No newline at end of file'