Native: Function

Function Methods.

Смотрите также:

Function Method: create

Base function for creating functional closures which is used by all other Function prototypes.

Синтаксис:

var createdFunction = myFunction.create([options]);

Аргументы:

  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.

Options:

  • bind - (object: defaults to this function) The object that the "this" of the function will refer to.
  • 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.
  • 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.
  • 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.
  • 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.
  • attempt - (boolean: false) If set to true, the returned function will try to execute and return either the results or null on error.

Возвращает:

  • (function) The function that was created as a result of the options passed in.

Пример:

var myFunction = function(){
    alert("I'm a function. :D");

};
 
var mySimpleFunction = myFunction.create(); //Just a simple copy.
 
var myAdvancedFunction = myFunction.create({ //When called, this function will attempt.

    Аргументы: [0,1,2,3],
    attempt: true,
    delay: 1000,
    bind: myElement
});

Function Method: pass

Returns a closure with arguments and bind.

Синтаксис:

var newFunction = myFunction.pass([args[, bind]]);

Аргументы:

  1. args - (mixed, optional) The arguments to pass to the function (must be an array if passing more than one argument).
  2. bind - (object, optional) The object that the "this" of the function will refer to.

Возвращает:

  • (function) The function whose arguments are passed when called.

Пример:

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.

Function Method: attempt

Tries to execute the function.

Синтаксис:

var result = myFunction.attempt([args[, bind]]);

Аргументы:

  1. args - (mixed, optional) The arguments to pass to the function (must be an array if passing more than one argument).
  2. bind - (object, optional) The object that the "this" of the function will refer to.

Возвращает:

  • (mixed) The function's return value or null if an exception is thrown.

Пример:

var myObject = {

    'cow': 'moo!'
};
 
var myFunction = function(){
    for (var i = 0; i < arguments.length; i++){

        if(!this[arguments[i]]) throw('doh!');
    }

};
var result = myFunction.attempt(['pig', 'cow'], myObject); //result = null

Function Method: bind

Changes the scope of this within the target function to refer to the bind parameter.

Синтаксис:

myFunction.bind([bind[, args[, evt]]]);

Аргументы:

  1. bind - (object, optional) The object that the "this" of the function will refer to.
  2. args - (mixed, optional) The arguments to pass to the function (must be an array if passing more than one argument).

Возвращает:

  • (function) The bound function.

Пример:

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(); //This will make myElement's text red.

Function Method: bindWithEvent

Changes the scope of this within the target function to refer to the bind parameter. It also makes "space" for an event. This allows the function to be used in conjunction with Element:addEvent and arguments.

Синтаксис:

myFunction.bindWithEvent([bind[, args[, evt]]]);

Аргументы:

  1. bind - (object, optional) The object that the "this" of the function will refer to.
  2. args - (mixed, optional) The arguments to pass to the function (must be an array if passing more than one argument).

Возвращает:

  • (function) The bound function.

Пример:

function myFunction(e, add){

    //Note that 'this' here refers to window, not an element.
    //We'll need to bind this function to the element we want to alter.
    this.setStyle('top', e.client.x + add);

};
$(myElement).addEvent('click', myFunction.bindWithEvent(myElement, 100));

//When clicked, the element will move to the position of the mouse + 100.

Function Method: delay

Delays the execution of a function by a specified duration.

Синтаксис:

var timeoutID = myFunction.delay([delay[, bind[, args]]]);

Аргументы:

  1. delay - (number, optional) The duration to wait (in milliseconds).
  2. bind - (object, optional) The object that the "this" of the function will refer to.
  3. args - (mixed, optional) The arguments passed (must be an array if the arguments are greater than one).

Возвращает:

  • (number) The JavaScript timeout id (for clearing delays).

Пример:

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);

Смотрите также:

Function Method: periodical

Executes a function in the specified intervals of time. Periodic execution can be stopped using the $clear function.

Синтаксис:

var intervalID = myFunction.periodical([period[, bind[, args]]]);

Аргументы:

  1. period - (number, optional) The duration of the intervals between executions.
  2. bind - (object, optional) The object that the "this" of the function will refer to.
  3. args - (mixed, optional) The arguments passed (must be an array if the arguments are greater than one).

Возвращает:

  • (number) The Interval id (for clearing a periodical).

Пример:

var Site = { counter: 0 };

var addCount = function(){ this.counter++; };
addCount.periodical(1000, Site); //Will add the number of seconds at the Site.

Смотрите также:

Function Method: run

Runs the Function with specified arguments and binding. The same as apply but reversed and with support for a single argument.

Синтаксис:

var myFunctionResult = myFunction.run(args[, bind]);

Аргументы:

  1. args - (mixed) An argument, or array of arguments to run the function with.
  2. bind - (object, optional) The object that the "this" of the function will refer to.

Возвращает:

  • (mixed) This Function's return value.

Примеры:

Simple Run:

var myFn = function(a, b, c){

    return a + b + c;
}
var myArgs = [1,2,3];
myFn.run(myArgs); //Возвращает: 6

Run With Binding:

var myFn = function(a, b, c) {
    return a + b + c + this;

}
var myArgs = [1,2,3];
myFn.run(myArgs, 6); //Возвращает: 12


Эта документация распостраняется на правах Attribution-NonCommercial-ShareAlike 3.0 License.
Оригинал документации на английском.
© Linux.ria.ua, 2008-2024