Function Methods.
If the passed argument is a function, it will return itself. Otherwise, it will return a function that returns the passed argument.
var foo = Function.from(obj);
var fn = Function.from(42); alert(fn()); // alerts '42' var fn2 = Function.from(fn); alert(fn2()); // alerts '42'
This function is equivalent to the following deprecated MooTools 1.2 methods:
var fn1 = Function.from(); // equivalent to var fn1 = function(){}; var fn2 = Function.from(foo); // equivalent to var fn2 = function(){ return foo; };
Tries to execute a number of functions. Returns immediately the return value of the first non-failed function without executing successive functions, or null.
Function.attempt(fn[, fn, fn, fn, ...]);
null
if all the passed functions fail.var result = Function.attempt(function(){ return some.made.up.object; }, function(){ return jibberish.that.doesnt.exists; }, function(){ return false; }); //result is false var failure, success; Function.attempt(function(){ some.made.up.object = 'something'; success = true; }, function(){ failure = true; }); if (success) alert('yey!');
This method is an equivalent of $try from MooTools 1.2.
Extends a function with a new method or property.
myFunction.extend(key, value); // Or myFunction.extend(object);
Or
var myFunction = function(){}; myFunction.extend('alert', function(text){ alert(text); }); myFunction.alert('Hello!'); // alerts Hello! // Using objects myFunction.extend({ alert: function(text){ alert(text); } });
Implements a method to the prototype of the function.
myFunction.implement(key, value); // Or myFunction.implement(object);
Or
var myFunction = function(){}; myFunction.implement('alert', function(text){ alert(text); }); var myInstance = new myFunction(); myInstance.alert('Hello!'); // alerts Hello! // Using objects myInstance.implement({ alert: function(text){ alert(text); } });
The difference between implement and extend, is that implement adds the value to the prototype. So with implement each instance of the function will have this method or property while with extend the method or property is added to a single instance.
Tries to execute a single function. Returns immediately the return value of the function if it does not fail, or null.
var myFunctionResult = myFunction.attempt(args[, bind]);
null
if the function fails.var myFunction = function(){ return some.made.up.object; }; myFunction.attempt(); // returns 'null' var myFunction = function(val){ return val; }; myFunction.attempt(false); // returns 'false'
Returns a closure with arguments and bind.
var newFunction = myFunction.pass([args[, bind]]);
var myFunction = function(){ var result = 'Passed: '; for (var i = 0, l = arguments.length; i < l; i++){ result += (arguments[i] + ' '); } return result; } var myHello = myFunction.pass('hello'); var myItems = myFunction.pass(['peach', 'apple', 'orange']); // Later in the code, the functions can be executed: alert(myHello()); // passes 'hello' to myFunction. alert(myItems()); // passes the array of items to myFunction.
Changes the scope of this
within the target function to refer to the bind parameter.
myFunction.bind([bind[, arg1, arg2, ...]]);
function myFunction(){ // Note that 'this' here refers to window, not an element. // the function must be bound to the element we want to manipulate. this.setStyle('color', 'red'); }; var myBoundFunction = myFunction.bind(myElement); myBoundFunction(); // makes myElement's text red // To show how bind works the following example: var myBoundFunction = myFunction.bind(anyVar); // is roughly equivalent with var myBoundFunction = function(){ return myFunction.call(this); };
Delays the execution of a function by a specified duration.
var timeoutID = myFunction.delay(delay[, bind[, args]]);
var myFunction = function(){ alert('moo! Element id is: ' + this.id); }; //wait 50 milliseconds, then call myFunction and bind myElement to it myFunction.delay(50, myElement); // alerts: 'moo! Element id is: ... ' //an anonymous function which waits a second and then alerts (function(){ alert('one second later...'); }).delay(1000); //to stop the delay, clearTimeout can be used like so: var timer = myFunction.delay(50); clearTimeout(timer);
Executes a function in the specified intervals of time. Periodic execution can be stopped using the clearInterval function.
var intervalID = myFunction.periodical(period[, bind[, args]]);
var Site = { counter: 0 }; var addCount = function(){ this.counter++; }; addCount.periodical(1000, Site); //adds the number of seconds at the Site. // the interval can be stopped using the clearInterval function var timer = myFunction.periodical(1000); clearInterval(timer);
This function has been deprecated.
This function has been deprecated.
myElement.addEvent('click', function(e){ myFunction.call(bind, e); });
This function has been deprecated.
fn.apply(thisArg, arguments); // Old API: fn.run(arguments, thisArg);
© Linux.ria.ua, 2008-2024 |