0

I want to implement a reusable helper function which can change the name of a variable to string value. For example, I have some parameters, the function will print which one is empty:

paramsHandler(name, address, mobileNum, ...){
  if(!name){
    console.log("name cannot be empty");
  }
  if(!address){
    console.log("address cannot be empty");
  }  
  if(!mobileNum){
    console.log("mobileNum cannot be empty");
  }
  ...
}

Now, I want to make it generalized, something like below, where key should be the string of the parameter name.

paramsHandler(...params){
  for(const key of params){
      if(!key){
         console.log("key cannot be empty");
      }
  }
}

Do anyone knows how could I do this?

leo0807
  • 941
  • 1
  • 8
  • 21
  • 4
    _"...where key should be the string of the parameter name"_ - You don't have named parameters in that example. `params` is an array. So the best you can get is an index. – Andreas Feb 18 '22 at 15:18
  • 1
    You can examine the `arguments` object, but when you use the `... params` form then the parameters *have no names*. – Pointy Feb 18 '22 at 15:18
  • 2
    @Pointy neither do the items in `arguments`. – VLAZ Feb 18 '22 at 15:23
  • 2
    @VLAZ yes, of course; if the formal parameters aren't named, then the best one can do is complain by position. The whole point of this question is unclear. – Pointy Feb 18 '22 at 15:29

0 Answers0