Search code examples
javascriptparametersargumentsnamed

Using named arguments in Javascript with regular expressions as names of arguments


Is it possible to use named arguments in Java or Javascript with regular expressions as the names of arguments? I want to make it possible to call a function like this:

f("function name:", "drawCircle", "radius:" 1, "xPos:" 0, "yPos:", 0, "color:", "red");

Or like this, with exactly the same effect:

f("name of function:", "draw a circle", "y position:", 0, "color:", "red", "rad:" 1, "x location:" 0);

Both of these should be equivalent to foo(1, 0, 0, red).

In both cases, the arguments that are given should match a list of regular expressions. It should be possible to list the arguments and the function name in any order with the same result.

Is there any way to implement something like this?


Solution

  • f({
      "name": "drawCircle", 
      "radius": 1, 
      "xPos": 0, 
      "yPos": 0, 
      "color": "red"
    });
    

    This is why you use objects in javascript.

    Although you probably want

    drawCircle({
      radius: 0,
      x: 0,
      y: 0,
      color: "red"
    });