Created
April 12, 2013 14:37
-
-
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...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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