1

I have the following javascript function whose parameter names I need to get dynamically

    static checkNotNull() {
        for (let arg of arguments) {
            if (arg === undefined || arg === null || arg === "") {
                return `${this._params(function(arg){})} is missing`;
            }
        }
    }

    static _params(func) {
      var args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1];
      return args.split(',').map(function (arg) {
        return arg.replace(/\/\*.*\*\//, '').trim();
      }).filter(function (arg) {
        return arg;
      });
}

I wish to know the name of the arg that was passed while iterating through the arguments object. I saw this question in stackoverflow How to get function parameter names/values dynamically? but it still couldn't solve my problem for my current use case.

Is there a work around for my current use case?

ololo
  • 1,326
  • 2
  • 14
  • 47
  • Depending on your use case you can use an object and `Object.entries()` to get the names of your 'arguments'. The function would only use one real argument then, but the process is less obscure. – ccarstens Mar 10 '21 at 08:03
  • @ccarstens Yea, but I don't want to use an object. That would require too much typing. – ololo Mar 10 '21 at 08:05
  • typing as in writing code or typing as in declaring data types? – ccarstens Mar 10 '21 at 08:10
  • @ccarstens writing code. Because, I have to check quite a lot of different parameter names dynamically – ololo Mar 10 '21 at 08:13
  • it's obviously up to you but that sounds like the code is going to be really hard to maintain or refactor. What problem are you trying to solve with dynamically named parameters? It's very likely that there will be a better way of doing that. – ccarstens Mar 10 '21 at 11:18

0 Answers0