Is there some way to make the following work in javascript?
var orders = [
{milk: true, sugar: "extra"},
{milk: false, sugar: "normal"}
];
function makeCoffee(sugar, milk) {
console.log(sugar);
console.log(milk);
}
orders.forEach(makeCoffee);
As I said in my comment, in browsers where
func.toString()returns the source of the function †, you could parse the signature and extract the parameter names:Since JavaScript does not provide any reflection API, this is probably the only way.
If you don’t explicitly state in which browsers your code is supported, this might not work (I don’t know what IE returns for
func.toString). I still recommend to pass an object as argument.†: The exact representation of the function is implementation dependent though. Here is the corresponding description from the specification: