Function

RightJS extends the Function unit prototype to provide the standard FP features like binding, curring and chains. It also adds a couple of methods for nicer delayed and periodical calls handling.

Methods

include, bind, bindAsEventListener, chain, curry, delay, periodical, rcurry

top ↑include

Function.include(Object new_methods[, Boolean dont_overwrite) -> Function

Description

Registers new functionality for Function instances

If the second argument is true, then the method will skip already existing methods

Example

Function.include({
double: function() {
return this.call() * 2;
}
});

var two = function() {
return 2;
};

two.double(); // -> 4

top ↑bind

bind(Object scope[, argument,...]) -> Function new

Description

Binds the function to be executed in the specified context. Additionally, it can take optional curry arguments:

Example

var object = {
attr: 'value'
};
var func = function(arg1, arg2) {
return this.attr + ': '+arg1 + ': '+ arg2;
};

var bind = func.bind(object, 'argument1');

var result = bind('argument2');

// -> 'value: argument1: argument2'

top ↑bindAsEventListener

bindAsEventListener(Object scope[, argument, ...]) -> Function new

Description

Binds the function to the given scope as if it was handling the first argument as an event:

Example

var object = {
attr: 'value'
};
var func = function(event, arg1, arg2) {
return this.attr +' > '+ event.eventName +' > '+ arg1 +' > '+ arg2;
};

$('some-element')
.onClick(
func.bindAsEventListener(object, 'argument1', 'argument2')
)
.fire('click');

// -> 'value > click > argument1 > argument2'

top ↑chain

chain(Function func[, value, ...]) -> Function new

Description

Schedules the argument function to be called immediately after the main function. Optionally with some pre-bound arguments:

Example

var f1 = function(list, num) { list.push(num); };
var f2 = function(list, num) { list.push(num); };
var f3 = function(list, num) { list.push(num); };

var list = []; // <- will track the calls

var f = f1.chain(f2, list, 2).chain(f3, list, 3);

f(list, 1); // calls the first function

list // -> [1, 2, 3]

top ↑curry

curry(mixed value[, mixed value, ...]) -> Function new

Description

The standard functional approach currying feature.

Example

var multiplier = function(x, y) {
return x * y
};
var double = multiplier.curry(2);

double(2); // 4
double(4); // 8
double(8); // 16

top ↑delay

delay(Integer timeout) -> Number timeout marker

Description

Schedules the delayed execution of the function after timeout milliseconds have expired.

NOTE: This method returns a timeout pointer. Call the cancel() method on this pointer to abort the timer

Example

var func = function() {
alert('boo');
};

var timeout = func.delay(2000);

// You should see the boo in 2 seconds.

// Cancel the delayed execution like this:
window.clearTimeout(timeout);

// Or like that:
timeout.cancel();

top ↑periodical

periodical(Integer timeout) -> Number timeout marker

Description

Schedule the function to be periodically executed every timeout milliseconds.

NOTE: This method returns a timeout pointer. Call the stop() method on this pointer to stop the periodical execution.

Example

var func = function() {
// check something
};

var marker = func.periodical(4000);

// Now the function will get executed every 4 seconds.

// Stop the periodical execution like this:
window.clearInterval(marker);

// Or like that:
marker.stop();

top ↑rcurry

rcurry(mixed value[, value, ...]) -> Function new

Description

The standard functional approach right currying feature

Example

var callback = function() { return $A(arguments); };

var with_one = callback.rcurry(1);
var with_two = callback.rcurry(1,2);

with_one(1); // -> [1,1]
with_one(1,2); // -> [1,2,1]

with_two(1); // -> [1,1,2]
with_two(1,2,3); // -> [1,2,3,1,2]