r/coffeescript • u/xjohnseanx • Nov 14 '14
Passing functions as parameters
Hey I am working through http://eloquentjavascript.net/ and decided to use coffeescript. However, I am having some issues getting a few of the examples to work.
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
forEach(["Wampeter", "Foma", "Granfalloon"], console.log);
// → Wampeter
// → Foma
// → GranfalloonHow would you write something like this using coffeescript? I already tried http://js2coffee.org/ after not being able to come up with a solution myself
4
Upvotes
1
u/xjohnseanx Nov 14 '14
I was able to create a slightly long solution but it seems to work
2
u/Corkscreewe Nov 14 '14
I usually use plain old for loop:
Or, if IE8 is not an issue (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map), I use Array.prototype.map:
1
u/brotherwayne Nov 14 '14
How about an IIFE (the
dopattern)? I don't have coffee handy, but this might help: