I'm writing a function which will allow me to display the values of an array by passing another function as a value.
This code works just fine:
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
forEach(["foo", "bar", "qux"], alert);
But it throws and error if I try to pass a function with a property:
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
forEach(["foo", "bar", "qux"], console.log);
If I do this:
action.log(array[i]);
it works just fine, but that would only allow me to pass functions with a .log property. Is there any way I can write a function which allows me to pass another function and property as a value?
this/ context inside a callback? - passconsole.log.bind(console)instead.