G
Guest
trying to shorten up code by passing a function name as a parameter.
Here is what it looks like:
function doSomething(function myFunctionName()) {
// some code like:
// DB.createAdapter
//DB.attachAdapter
// DB.isOpen
// do this function - which does something different every time
// like fetch names the first time, fetch users the 2nd, fetch ID the 3rd,
etc...
myFunctionName();
}
this way, i can easily save 5-10 lines of db connection code by simply
passing in a new function instead of creating the adapter and connection over
and over.
Instead the new code would look like so:
// do DB code and run function passed
users = doSomething(fetchUsers());
names = doSomething(fetchNames());
ids = doSomething(fetchIDs());
is this possible? reason i want to do this is because i have so many copy
and pasted lines of the same code and instead of taking up so much space i'd
like to just use those lines of code before and after a function that does
something different!
please help. thanks in advance.
Here is what it looks like:
function doSomething(function myFunctionName()) {
// some code like:
// DB.createAdapter
//DB.attachAdapter
// DB.isOpen
// do this function - which does something different every time
// like fetch names the first time, fetch users the 2nd, fetch ID the 3rd,
etc...
myFunctionName();
}
this way, i can easily save 5-10 lines of db connection code by simply
passing in a new function instead of creating the adapter and connection over
and over.
Instead the new code would look like so:
// do DB code and run function passed
users = doSomething(fetchUsers());
names = doSomething(fetchNames());
ids = doSomething(fetchIDs());
is this possible? reason i want to do this is because i have so many copy
and pasted lines of the same code and instead of taking up so much space i'd
like to just use those lines of code before and after a function that does
something different!
please help. thanks in advance.