-2

If you are dynamically generating functions with varying number of required arguments, how do you determine if the future number of supplied arguments to the function call are enough and/or how do you make sure your function warns for / about required, or missing number of arguments?

The anonymous function may have a structure body as simple as:

fucntion( x, y, z ) {
        /*test for the arguments supplied*/
    if( notEnoughArguments )
        /*exit the function and return a warning*/   
        /*otherwise, say ...*/
     return x*y*x
    }

The problem: you don't know beforehand how many arguments will a certain function require, and you're not sure how many arguments will be available/supplied at the time of the function call.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Bekim Bacaj
  • 5,707
  • 2
  • 24
  • 26
  • You can check for `arguments.length < 3` to makes sure, ad least three arguments were provided. You can `throw new Error('missing arguments')` to throw an Exception and abort or log to console, if you don`t want to abort. – Syntac Dec 09 '17 at 09:48
  • 1
    maybe dupe: https://stackoverflow.com/questions/2141520/javascript-variable-number-of-arguments-to-function – Nina Scholz Dec 09 '17 at 09:50
  • ok, it was just a question. – Nina Scholz Dec 09 '17 at 09:56
  • To me it is a very valid question and different from the proposed duplicate. – Syntac Dec 09 '17 at 10:04
  • if i would not like the question, i would dv it, if i think it's a dupe, i could close it directly. anyway `arguments.length` is a property of function. – Nina Scholz Dec 09 '17 at 10:04

2 Answers2

0

You can use arguments object inside the function. For e.g. arguments.length will provide number of arguments passed into the function. Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments for more details

cdoshi
  • 2,772
  • 1
  • 13
  • 20
0

You can check the arguments length vs. the Function.length.

In functions...

The length property specifies the number of arguments expected by the function.

The arguments object is an array like object, that contains all arguments passed to the function.

function multi(x, y, z) {
  if(arguments.length < multi.length) {
    console.log('not enough arugments');
    return null;
  }

  return x * y * x
}

multi();

console.log(multi(1, 2, 3));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • That'd be great, but unless there is a way to generate named functions the functions generated will be anonymous. – Bekim Bacaj Dec 09 '17 at 09:53
  • You can always add a name to a function - even anonymous ones. `var a = function b();` -> the internal name of the function is `b`. It works in callbacks as well -> `.map(function m() {})`. – Ori Drori Dec 09 '17 at 09:55
  • You might want to tell mdn's [arguments article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) about that. – Ori Drori Dec 09 '17 at 10:10
  • 1
    @BekimBacaj - feel free to supply your own references. However, you are digressing. Does the answer helps you in your problem or not? If not, why? – Ori Drori Dec 09 '17 at 10:35