1

I want to create a function such as:

var func = function(arg1, arg2) {
    callAnotherFunc(arg1, arg2);
}

as you can see, when someone needs to call func, it needs to pass 2 args. sometimes, arg2 can be null.

Sometimes, arg2 will be null. Is there some shortcut which allows me to do this ?

var func = function(arg1, arg2) {
    callAnotherFunc(arg1, arg2 || nothing);
}

So if arg2 is null, it shouldn't pass another argument to callAnotherFunc at all. I am looking for some shortcut and not if/else

halfer
  • 19,824
  • 17
  • 99
  • 186
Nika Kurashvili
  • 6,006
  • 8
  • 57
  • 123
  • 1
    Does this answer your question? [Set a default parameter value for a JavaScript function](https://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function) – Bagus Tesa Mar 03 '22 at 08:48
  • You could also use [`arguments`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments). – Lain Mar 03 '22 at 08:54
  • Would a simple `if` not be faster and more clear in the end? Watching this from readability and maintainability perspectives, I would just stick with it. – JavaScript Mar 03 '22 at 10:42

6 Answers6

0

You can try something like this

How To Use ES6 Arguments And Parameters

var func = (...args) => {
    callAnotherFunc(...args);
}

var callAnotherFunc = (...args) =>{
  console.log(...args)
}

func(1);

func(1,2);

func(1,2,3);
Raycas
  • 151
  • 1
  • 8
  • Looks nice. Yet changes the signature of `func` an does not really regard `So if arg2 is null, it shouldn't pass another argument to callAnotherFunc at all.` – JavaScript Mar 03 '22 at 09:24
  • you can filter the args when you call the `callAnotherFunc(...args.filter(arg => arg !== null))` – Raycas Mar 03 '22 at 13:35
  • You can, but you are not doing it. Also this would omit the first argument as well. – JavaScript Mar 04 '22 at 07:30
0

You could forward all arguments which are not null by using call.

var callAnotherFunc = function(){
  console.log(arguments)
};

var func = function(arg1, arg2){
    //So if arg2 is null, it shouldn't pass another argument to callAnotherFunc at all. 
    callAnotherFunc.call(
      null,
      Array.from(arguments).filter(function(item, index){
        return index == 0 || item !== null
      })
    )
};

func(1, 2);
func(1, null); //REM: Does not pass second argument
func(null, null); //REM: Does not pass second argument
Lain
  • 3,657
  • 1
  • 20
  • 27
0

I don't understand why you would want to this, maybe your intentions are beyond my understanding. You can just use default parameters

var func = function(arg1, arg2 = null) {
    callAnotherFunc(arg1, arg2);
}

var callAnotherFunc = function(arg1, arg2 = null){
    // console.log(arg1);
    // console.log(arg2);
}
tenshi
  • 508
  • 4
  • 12
-1

No, there isn't. Just use if / else.

Simon Brahan
  • 2,016
  • 1
  • 14
  • 22
-1
var func = function(...args) {
    // args will be an array of all the arguments
    if (args[1] == null) args.splice(1, 1);
    callAnotherFunc.apply(this, args);
}
  • 2
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Mar 03 '22 at 12:18
  • Thanks Luca for this suggestion. I will try to improve the answer quality and explanation in future. – Shivam Mishra Mar 03 '22 at 13:49
-1

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

var func = function(arg1, arg2) {
    arg2 && func1(arg1, arg2);
}

func(1, 2);

func(1); // not consoled