I know that a CoffeeScript class can be extended like this:
Dog::bark = ->
console.log("bark")
I want to be able to do this dynamically. For example, I want to do something like this:
sounds = [ "bark", "woof", "grrr", "ruff" ]
for sound in sounds
Dog::[sound] = ->
console.log(sound)
The equivalent JavaScript would be:
var sounds = [ "bark", "woof", "grrr", "ruff" ];
for (var i = 0; i < sounds.length; i++)
{
var sound = sounds[i];
Dog.prototype[sound] = function() {
console.log(sound);
};
}
How can I do this with CoffeeScript?