0

Using this function def: myFunc(p1, p2, p3 = 3, p4 = 4){ //do something }

I understand that you can call a function using Apply, Like this: myFunc.apply([1, 2])

Where 1 is p1 and 2 is p2.

But how would one use apply when specifying a specific optional param name?:

myFunc(1, 2, p4=>5)

Thanks!

Kenobi
  • 465
  • 6
  • 13
  • JavaScript doesn't have named parameters in the first place. `p4=>5` is an arrow function, short for `function(p4) { return 5; }`. – Barmar Sep 06 '18 at 15:23

3 Answers3

4

In the same way you would by calling it directly. When calling a function, to skip over a default parameter, you pass undefined:

myFunc(1, 2, undefined, 4);

You can do the same thing using apply, just pass in undefined when you want the default parameter to kick in:

function myFunc(p1, p2, p3 = 3, p4 = 4) {
    console.log("p1:", p1);
    console.log("p2:", p2);
    console.log("p3:", p3);
    console.log("p4:", p4);
}

myFunc.apply(null, [11, 22, undefined, 44]);   // skips over p3
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • The same thing happened with my answer. I'm pretty positive it's just pettiness for unknowingly answering a dup question. *shrug* – zfrisch Sep 06 '18 at 15:41
  • 1
    @zfrisch Even fabio's answer and the question itself got a downvote. And the question is no longer marked as a duplicate because I've reopened it. The downvote happened afterwards, so I guess it's just a serial downvoter. – ibrahim mahrir Sep 06 '18 at 15:45
  • 1
    I like this for simplicity. But I'm going with sfrisch answer because it allows me to pass in only what I need to pass in and nothing more, ie, no undefined. – Kenobi Sep 06 '18 at 16:16
2

In JavaScript to pass specific named parameters you would need to pass an object. You can use object destructuring to get the desired result.

{p1: 1, p2: 2, p4: 5}

function myFunc({p1, p2, p3 = 3, p4 = 4}){ console.log(p3); }


myFunc.apply(null, [{p1: 1, p2: 2, p4: 5}]);
fabio.sussetto
  • 6,964
  • 3
  • 25
  • 37
zfrisch
  • 8,474
  • 1
  • 22
  • 34
  • 1
    If the person who cast a downvote could explain why I would appreciate it. I get the idea that they probably did so because it was a duplicate question, but I obviously wasn't aware or I wouldn't have answered. That doesn't make my answer less valid. – zfrisch Sep 06 '18 at 15:37
  • Yes! That's what I need. – Kenobi Sep 06 '18 at 16:17
0

Javascript doesn't have named arguments (ES6 included). So as you can guess, you can only use apply with positional arguments, so there's no way to use apply the way you want to.

In ES6, a way to kind of emulate named args is to use an object, for example:

myFunc({ from=3, to=5 });

Example with the technique above and apply (ES6):

let f = ({ p1='a', p2='b'} = {}) => { console.log(p1, p2); }
// logs 'a', 'b'
f.apply(this, [{ p1: 'c' }])
// logs 'c', 'b'

You can read more here: http://2ality.com/2011/11/keyword-parameters.html

fabio.sussetto
  • 6,964
  • 3
  • 25
  • 37
  • Looks like you copied that example from http://2ality.com/2011/11/keyword-parameters.html and just changed the function name. You should probably add attribution. – Barmar Sep 06 '18 at 15:27
  • I also ran into it while trying to make sure that his syntax didn't really exist, it was still on my screen while I read your answer. :) – Barmar Sep 06 '18 at 15:33