0

I have no idea why I can't find an answer to this, I feel kinda ridiculous for even asking but it's killing me.

Can function.modifier params be specified via iteration?

let colors = ['red', 'green', 'blue];

Example function uses .modifier(argument) as styling param. (real-world ex: react-toastify)

function alert.red() // red widget
function alert.blue() // blue widget
function alert.green() // green widget

but colors.forEach(color => alert.{color}('arg')) // error ??

.. I mean.. seriously? Am I missing something?

AveryFreeman
  • 1,061
  • 2
  • 14
  • 23

1 Answers1

3

Since the red, blue and green functions as mere properties of alert, you can just call them like so: alert[color]('arg')

const testAlert = {
  blue: function(message) {
     console.log('blue ' + message);
  },
  red: function(message) {
     console.log('red ' + message);
  }
};

['blue', 'red'].forEach(function(color) {
   testAlert[color]('message');
});
user125661
  • 1,558
  • 12
  • 28