0

I have a javascript function with three default parameters.

function myFunction(first, second='default second variable',  third=function(){console.log('default third function')}, forth=function(){console.log('default forth function')}) {
    console.log(first);
    console.log(second);
    third();
    forth();
}

myFunction('hello', function(){console.log('next')});

I want to call myFunction with the first and third parameter, however it seems i can only pass the two arguments for two consecutive parameters and must supply an argument for the first parameter. Hence, the second function argument is printed as-is and is never evaluated as a function.

Questions:

1. How does ECMAScript 2015 (ES6) select arguments for default parameters?

2. Can I specify the first parameter as default where I have multiple parameters?

3. Also, can I assign arguments to non-consecutive parameters?

Udo E.
  • 2,665
  • 2
  • 21
  • 33
  • 1
    You only pass the first and second argument. Pass `undefined` to skip an argument, then it will go to default. Try `myFunction('hello', undefined, function(){console.log('next')});`. – StackSlave Aug 08 '19 at 03:22
  • 1
    I'd strongly recommend reading [this answer about "overloading functions" in Javascript](https://stackoverflow.com/questions/10855908/how-to-overload-functions-in-javascript/10855939#10855939) which describes the various options of **variable argument**s, **default arguments** and **named arguments**. – jfriend00 Aug 08 '19 at 03:52
  • Implementing variable arguments requires code checking the types of arguments to try to figure out which arguments were passed and which were left out and when two arguments are of the same type (indistinguishable from one another), you can't pass the 2nd one and not the 1st as there's no way for the code to know which one you intended to pass. The details of how you do this are all described in the above answer reference. – jfriend00 Aug 08 '19 at 03:56
  • To be able to pass any set of arguments and leave out any set of arguments, you need to pass an object with named properties as the arguments. Then, the code can tell exactly which named arguments were and weren't passed and you can pass any meaningful set of arguments and leave out the others and the underlying function can tell exactly what was intended. – jfriend00 Aug 08 '19 at 03:56

2 Answers2

2

why don't you use key-value object as parameter

function myFunction({ first, second="default 2nd variable", third= function() { console.log("default 3rd function") }, forth = function() { console.log("default 4th function") } }){
  console.log(first);
  console.log(second);
  third();
  forth();
}

myFunction({first: "hello", third: function() { console.log("3rd") } });
kyun
  • 9,710
  • 9
  • 31
  • 66
  • This would mean one "Big" Object parameter. No, I'm referring to a case of multiple parameters. – Udo E. Aug 08 '19 at 03:16
1

Parameters with default values need to be placed at the end of the parameter. in this case , you can post null as second parameter

XuWang
  • 179
  • 7
  • Prior to `ES6` you can specify the first parameter as default inside the function definition. So it should be possible with this notation – Udo E. Aug 08 '19 at 03:14