4

Let's say I have a function myfunc that can take up to two parameters: arg1 and arg2:

var myfunc = function (arg1,arg2) {
  console.log(arg1,arg2);
};

I want to call the function using only arg2. However, when I run this, the input is passed to the first argument, and myfunc will log "input" undefined rather than undefined "input" as expected.

myfunc(arg2 = 'input')
> "input" undefined

Does Javascript support this type of input?

jane
  • 385
  • 1
  • 4
  • 14
  • 3
    do you have a use case for this? – Nina Scholz Mar 17 '19 at 15:51
  • 3
    Why is this downvoted? The question is clear, he attempts something, what's wrong with it? – Radvylf Programs Mar 17 '19 at 15:52
  • 1
    Javascript does not support named parameters. Pass an object if you want something similar. – Reactgular Mar 17 '19 at 15:52
  • If arg1 and arg2 are known types distinct from one another, you could use `function (...args)` so you can handle them from the array `args` – Scrimothy Mar 17 '19 at 15:55
  • you could use `if (argument.length === 1) [arg1, arg2] = [arg2, arg1];` where you check the length of the handed opver parameters and swap the arguments. – Nina Scholz Mar 17 '19 at 15:58
  • You could do something like function callWithPositions(f, args) { const realArgs = Array(Math.max(Object.keys(args)) + 1).fill(0).map((_, i) => args[i]); f(...realArgs); } callWithPositions(myFunc, { 1: "input" }); – Countingstuff Mar 17 '19 at 16:02
  • @NinaScholz I have an array that I want to access either by the index number, or by a field associated with each object in the array. So swapping the arguments wouldn't work either, since I also want to allow passing only `arg1`. – jane Mar 17 '19 at 16:16
  • 1
    @jane so both parameters are optional and you could call the function with either one or both of them? Are you sure you want to use a single function and/or two parameters for this? It seems like you probably want two different functions or at the very least a parameter *object* which has two optional fields. – VLAZ Mar 17 '19 at 16:22

3 Answers3

5

Javascript doesnot support the parameters with name. But you can achieve same thing using Unpacking fields from objects passed as function parameter and passing object to function

var myfunc = function ({arg1,arg2}) {
  console.log(arg1,arg2);
};

myfunc({arg2:"input"})
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
2

By doing myFunc(arg2 = 'input) you're doing:

arg2 = 'input'
myFunc(arg2)

Solutions to make it works can be:

var myfunc = function (arg1='foo',arg2) {
  console.log(arg1,arg2);
}
myfunc('bar', 'input') 
myfunc(undefined, 'input') 

Another solution is to use object

var myfunc_es5 = function (opts) {
  opts = opts || {}
  console.log(opts.arg1,opts.arg2);
}
var myfunc_es6 = function ({arg1, arg2}) {
  console.log(arg1,arg2);
}
myfunc_es5 ({arg1: 'foo', arg2: 'bar'}) 
myfunc_es6 ({arg1: 'foo', arg2: 'bar'}) 
myfunc_es5 ({arg2: 'bar'}) 
myfunc_es6 ({arg2: 'bar'}) 
Arthur
  • 4,870
  • 3
  • 32
  • 57
0

If you want to act like you passed nothing to arg1, pass undefined.

Arguments are assigned undefined when nothing is passed to them.

This works with default arguments:

var func = function(a = 5){return a};
func(undefined); //5
Radvylf Programs
  • 1,429
  • 1
  • 12
  • 31