Class o2.Method


static class o2.Method

A method helper class.

Defined in method.core

Function Summary
static after (Integer count, Function delegate)

Creates a Function that only executes after being called count times.

Usage example:

 var bump = o2.Method.after(3, function() {
      console.log('bump');
 });

 bump();bump();bump();bump();
 // Will log 'bump' only once.
 
static bind (Object base, Function fn, Arguments varargin)

Creates a Function that uses base as the "this" reference.

bind can often be used to bind a different context to a curried function.

Usage example:

 function test(a,b,c){ return this.number + (a*b+c); };
 var context = {number:10};
 var bound = o2.Method.bind(context, test);
 bound(20,2,10);//gives 60
 var bound2 = o2.Method.bind(context, test, 20);
 bound2(2, 10);//gives 60
 

Binds the given function as an event listener, ensuring that the first parameter is always the event object.

This method is generally used in conjunction with o2.Event.addEventListener.

Usage example:

 var $ = o2.$;
 var listen = o2.Event.addEventListener;
 var getTarget = o2.Event.getTarget;
 var bind = o2.Method.bindAsEventListener;

 var context = {id : 1, description : 'hello world.'};
 var kClick = 'click';
 var menu = $('mainMenu');

 function test(evt, a, b) {
      console.log( getTarget(evt) );
      console.log( this.id );
      console.log( this.description );
      console.log( a );
      console.log( b );
 }

 var delegate = bind(context, test, 20, 30);
 listen(menu, kClick, delegate);

 // When mainMenu element is clicked the output will be as follows:
 //
 // [DOM Element]
 // 1
 // hello world
 // 20
 // 30
 
static compose (Function invoker, Function delegate)

Create a method that calls the invoker with the return value of the evaluated function fn. The current arguments are passed to fn, and the evaluated result is passed to the invoker.

Usage example:

 function double(a) { return a*2; }
 var min = Math.min;
 var doubleMin = o2.Method.compose(double, min);
 var res = doubleMin(3, 5);
 // res will be 6
 
static curry()

Curries the Function.

See http://www.dustindiaz.com/javascript-curry/ for a discussion.

Usage example:

 function test(a,b,c) { return a+b+c; }
 var curried = o2.Method.curry(this, test, 1, 2);
 var result = curried(3);//returns 6;
 
debounce (Function delegate, Integer waitMs)

Creates a Function that will not be triggered, as long as it continues to get invoked within a certain time window.

Usage example:

 var debounceAction = o2.Method.debuonce(function() {
      console('if you call me within a second, I will skip.');
 }, 1000);
 
static defer (Function delegate, Integer interval, Object context, Array args)

Defers a Function for a specified amount of time.

Usage example:

 var deferAction = o2.Method.defer(function() {
      console.log('I will be delayed for 1 second');
 }, 1000);
 
static delay()

An alias to o2.Method.defer.

static flip (Function fn, Integer index1, Integer index2)

Flips two arguments of the given Function, and returns a new Function.

Usage example:

 function factor(a, b) { return a / b; }
 var flipped = o2.Method.flip(factor, 0, 1);
 var result = factor(5, 2) == flipped(2, 5);
 // result will be true
 
static identity (Object value)

Just an identity function, that return what it's given without changing it.

Usage example:

 var result = o2.identity(4);
 // result will be 4
 
static memoize (Function fn, Object context, ... ...)

Memoizes the given Function's outcome and presents it from cache, instead of recalculating.

See http://en.wikipedia.org/wiki/Memoization for details.

Usage example:

 function multiply(a,b){return a*b; }
 var memoized = o2.Method.memoize(multiply);
 var result = memoized(2,3);//fresh calculation.
 result = memoized(4,2);//fresh calculation.
 result = memoized(2,3);//retrieve from cache.
 
static once (Function delegate)

Creates a Function that will only get called once.

May be useful for creating singleton Objects, or for lazy-loading modules.

Usage example:

 var init = o2.Method.once(function() {
      console.log('done');
 });

 init();init();
 // Will log 'done' only once.
 
static overload (Object object, String name, Function fn)

Adds a method to the Object.

If parameters count is different but the name is same, adds the method with a different signature, overloading the former one.

Usage example:

 var Context = {};

 o2.Method.overload(Context, 'test', function(a) {
      console.log(a);
 });

 o2.Method.overload(Context, 'test', function(a, b) {
      console.log(a + b);
 });

 Context.test(1);
 Context.test(1, 2);

 // will output:
 // 1
 // 3
 
static partial (Object base, Function fn, Arguments varargin)

Defines a partial Function.

See http://ejohn.org/blog/partial-functions-in-javascript/ for a detailed discussion.

Usage example:

 function test(a,b,c){ return a*b+c; }
 var partial = o2.Method.partial(this, test, 10, undefined, 20);
 var result = partial(3);//returns 50;
 
static requireAllArguments (Function fn)

Checks the passed in arguments, and if all arguments are present, executes Function. Otherwise throws an error.

Usage example:

 function test(a, b) {

 }

 var testEnsure = o2.Method.requireAllArguments(test);

 testEnsure(1);    // will throw an exception.
 testEnsure(1, 2); // will NOT throw an exception.
 
static throttle (Function delegate, Integer waitMs)

Returns a Function that will execute at most once in a given time window. That is to say, quick repetitive calls to the function are throttled.

This may be especially useful for asyncronous AJAX, requests, preventing the client to bombard the server with too many simultaneous requests.

Usage example:

 o2.Method.throttle(function() {
      console.log('You can call me at max once within a second');
 }, 1000);
 
static times (Integer count, Function delegate, Object context, Object payload)

Sequentially executes a given Function given amount of times.

Usage example:

 o2.Method.times(3, function(i) {
   console.log(i);
 });
 // Will log:
 // 0
 // 1
 // 2
 
static wrap (Function delegate, Function wrapper)

Returns the first delegate passed as an argument to the second wrapper followed by the arguments of the returned Funciton.

Usage example:

 function wrapper(fn, a, b) { return fn(a, b) + a + b; }
 function delegate { return a * b; }
 var wrapped = o2.Method.wrap(delegate, wrapper);

 var result = wrapped(3, 5);
 // result will be 23
 

Function Details

function after

static after(Integer count, Function delegate)

Creates a Function that only executes after being called count times.

Usage example:

 var bump = o2.Method.after(3, function() {
      console.log('bump');
 });

 bump();bump();bump();bump();
 // Will log 'bump' only once.
 
Parameters:
count - the numer of calls required to the Function before executing it.
delegate - the delegate to execute.
Returns:
a Function that will only execute after being called count times.

function bind

static bind(Object base, Function fn, Arguments varargin)

Creates a Function that uses base as the "this" reference.

bind can often be used to bind a different context to a curried function.

Usage example:

 function test(a,b,c){ return this.number + (a*b+c); };
 var context = {number:10};
 var bound = o2.Method.bind(context, test);
 bound(20,2,10);//gives 60
 var bound2 = o2.Method.bind(context, test, 20);
 bound2(2, 10);//gives 60
 
Parameters:
base - the context of the newly created Function.
fn - the Function to modify.
varargin - variable number of input arguments to be passed as initial set of arguments.
Returns:
the modified Function.

function bindAsEventListener

static bindAsEventListener()

Binds the given function as an event listener, ensuring that the first parameter is always the event object.

This method is generally used in conjunction with o2.Event.addEventListener.

Usage example:

 var $ = o2.$;
 var listen = o2.Event.addEventListener;
 var getTarget = o2.Event.getTarget;
 var bind = o2.Method.bindAsEventListener;

 var context = {id : 1, description : 'hello world.'};
 var kClick = 'click';
 var menu = $('mainMenu');

 function test(evt, a, b) {
      console.log( getTarget(evt) );
      console.log( this.id );
      console.log( this.description );
      console.log( a );
      console.log( b );
 }

 var delegate = bind(context, test, 20, 30);
 listen(menu, kClick, delegate);

 // When mainMenu element is clicked the output will be as follows:
 //
 // [DOM Element]
 // 1
 // hello world
 // 20
 // 30
 
Returns:
the bound Function.

function compose

static compose(Function invoker, Function delegate)

Create a method that calls the invoker with the return value of the evaluated function fn. The current arguments are passed to fn, and the evaluated result is passed to the invoker.

Usage example:

 function double(a) { return a*2; }
 var min = Math.min;
 var doubleMin = o2.Method.compose(double, min);
 var res = doubleMin(3, 5);
 // res will be 6
 
Parameters:
invoker - the invoker.
delegate - the invokee.
Returns:
the created Function.

function curry

static curry()

Curries the Function.

See http://www.dustindiaz.com/javascript-curry/ for a discussion.

Usage example:

 function test(a,b,c) { return a+b+c; }
 var curried = o2.Method.curry(this, test, 1, 2);
 var result = curried(3);//returns 6;
 
Returns:
the modified Function.

function debounce

debounce(Function delegate, Integer waitMs)

Creates a Function that will not be triggered, as long as it continues to get invoked within a certain time window.

Usage example:

 var debounceAction = o2.Method.debuonce(function() {
      console('if you call me within a second, I will skip.');
 }, 1000);
 
Parameters:
delegate - the Function to debounce.
waitMs - the least amount of time (in milliseconds) to wait between calls.
Returns:
the debounced Function.

function defer

static defer(Function delegate, Integer interval, Object context, Array args)

Defers a Function for a specified amount of time.

Usage example:

 var deferAction = o2.Method.defer(function() {
      console.log('I will be delayed for 1 second');
 }, 1000);
 
Parameters:
delegate - the Function to defer.
interval - the interval to defer in milliseconds.
context - the context (this reference) to bind.
args - arguments to pass to the function.

function delay

static delay()

An alias to o2.Method.defer.

See also:

function flip

static flip(Function fn, Integer index1, Integer index2)

Flips two arguments of the given Function, and returns a new Function.

Usage example:

 function factor(a, b) { return a / b; }
 var flipped = o2.Method.flip(factor, 0, 1);
 var result = factor(5, 2) == flipped(2, 5);
 // result will be true
 
Parameters:
fn - the delegate to flip arguments of.
index1 - the index of the first argument.
index2 - the index of the second argument.
Returns:
the created Function.

function identity

static identity(Object value)

Just an identity function, that return what it's given without changing it.

Usage example:

 var result = o2.identity(4);
 // result will be 4
 
Parameters:
value - input.
Returns:
the value itself.

function memoize

static memoize(Function fn, Object context, ... ...)

Memoizes the given Function's outcome and presents it from cache, instead of recalculating.

See http://en.wikipedia.org/wiki/Memoization for details.

Usage example:

 function multiply(a,b){return a*b; }
 var memoized = o2.Method.memoize(multiply);
 var result = memoized(2,3);//fresh calculation.
 result = memoized(4,2);//fresh calculation.
 result = memoized(2,3);//retrieve from cache.
 
Parameters:
fn - the Function to memoize.
context - what should "this" refer to.
... - variable number of input arguments to pass arguments to fn.
Returns:
a reference to the memoized Function.

function once

static once(Function delegate)

Creates a Function that will only get called once.

May be useful for creating singleton Objects, or for lazy-loading modules.

Usage example:

 var init = o2.Method.once(function() {
      console.log('done');
 });

 init();init();
 // Will log 'done' only once.
 
Parameters:
delegate - the Function to execute.
Returns:
a Function that will execute only once.

function overload

static overload(Object object, String name, Function fn)

Adds a method to the Object.

If parameters count is different but the name is same, adds the method with a different signature, overloading the former one.

Usage example:

 var Context = {};

 o2.Method.overload(Context, 'test', function(a) {
      console.log(a);
 });

 o2.Method.overload(Context, 'test', function(a, b) {
      console.log(a + b);
 });

 Context.test(1);
 Context.test(1, 2);

 // will output:
 // 1
 // 3
 
Parameters:
object - the Object to add methods to.
name - the name of the method.
fn - the method reference.
Returns:
the overloaded Function.

function partial

static partial(Object base, Function fn, Arguments varargin)

Defines a partial Function.

See http://ejohn.org/blog/partial-functions-in-javascript/ for a detailed discussion.

Usage example:

 function test(a,b,c){ return a*b+c; }
 var partial = o2.Method.partial(this, test, 10, undefined, 20);
 var result = partial(3);//returns 50;
 
Parameters:
base - the context of the newly created Function.
fn - the Function to modify.
varargin - variable number of input arguments to be passed as initial set of arguments.
Returns:
the modified Function.

function requireAllArguments

static requireAllArguments(Function fn)

Checks the passed in arguments, and if all arguments are present, executes Function. Otherwise throws an error.

Usage example:

 function test(a, b) {

 }

 var testEnsure = o2.Method.requireAllArguments(test);

 testEnsure(1);    // will throw an exception.
 testEnsure(1, 2); // will NOT throw an exception.
 
Parameters:
fn - the Function to check.
Returns:
the applied Function.
Throws:
Excpetion if all of the arguments is not provided to the Function.

function throttle

static throttle(Function delegate, Integer waitMs)

Returns a Function that will execute at most once in a given time window. That is to say, quick repetitive calls to the function are throttled.

This may be especially useful for asyncronous AJAX, requests, preventing the client to bombard the server with too many simultaneous requests.

Usage example:

 o2.Method.throttle(function() {
      console.log('You can call me at max once within a second');
 }, 1000);
 
Parameters:
delegate - the Function to throttle.
waitMs - the least amount of time (in milliseconds) to wait between calls.
Returns:
the throttled Function.

function times

static times(Integer count, Function delegate, Object context, Object payload)

Sequentially executes a given Function given amount of times.

Usage example:

 o2.Method.times(3, function(i) {
   console.log(i);
 });
 // Will log:
 // 0
 // 1
 // 2
 
Parameters:
count - number of times to execute.
delegate - the Function to execute (in the form function(i, payload)).
context - what should this refer to inside the Function.
payload - the Object to pass to the delegate as a second argument.

function wrap

static wrap(Function delegate, Function wrapper)

Returns the first delegate passed as an argument to the second wrapper followed by the arguments of the returned Funciton.

Usage example:

 function wrapper(fn, a, b) { return fn(a, b) + a + b; }
 function delegate { return a * b; }
 var wrapped = o2.Method.wrap(delegate, wrapper);

 var result = wrapped(3, 5);
 // result will be 23
 
Parameters:
delegate - the first Function to pass as parameter.
wrapper - the wrapper Function.
Returns:
the wrapped Function.