Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Created April 12, 2013 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telekosmos/5372464 to your computer and use it in GitHub Desktop.
Save telekosmos/5372464 to your computer and use it in GitHub Desktop.
Array of function objects with (potentially) asynchronous execution which keeps the result inside an object...
var sum = function (a, b, context) {
var res = a+b;
context.result = res;
};
var prod = function (a, b, context) {
var res = a*b;
context.result = res;
};
var funcObj1 = {
alias: 'addition',
func: sum,
result: undefined
};
var funcObj2 = {
alias: 'product',
func: prod,
result: undefined
};
var ops = [funcObj1, funcObj2];
var vals = [13, 5];
for (var i=0; i<ops.length; i++) {
ops[i].func(vals[0], vals[1], ops[i]);
}
for (var i=0; i<ops.length; i++) {
console.log('with context: '+ops[i].alias+'->'+ops[i].result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment