0

I want to handle in an one function two ways of passing parameters. 1) It should be a standard way with passing single parameter 2) It should be a way with an array destruct to fetch all passed parameters in this array.

Something like this:

function a (z, c) {
  console.log("z => ", z);
  console.log("c => ", c);
}


let b = [1,2];
a(...b);

It works fine and prints both console log out. But I also want to have a possibility to pass an one parameter. Something like that:

function a (z, c) {
  console.log("z => ", z);
  console.log("c => ", c);
}

//now varibale b  should contains a number, not an array.
//let b = [1,2];

// how can  I detect that here is an one parameter 
and I can't use the destructed parameter? How I can do this without changing a function call

let b = 1; 

// throws an error
a(...b); 

I can't assign the destruct operators result to some variable and test it's type via typeof etc. because in case of real destructing - it's impossible.

P.S. I know that the Destruct operator works only with arrays and it can't be applied to the Number. But it's just for an example.

I just wonder how I can pass or 1 parameter (like number) into a function or detect that that parameter is an array and I should fetch all array items and pass them into the function like in case with standard function call with multiple parameters

myFunc(1,2,3,4)

Maybe someone can help me? Thanks for any information.

UPDATE:

As like an ugly solution I can use simple if typechecking but I don't know how correct it will be.

function a (z, c) {
console.log("z => ", z);
console.log("c => ", c);

}

let b = 1;

if (~Object.prototype.toString.call(b).indexOf("Array")) {
a(...b);
} else {
a(b);
}
6
  • 1
    Why not simply a(b) ? Commented Jul 11, 2017 at 18:47
  • Can't you call a(b) and then check if b is an array within your function? Commented Jul 11, 2017 at 18:47
  • In my case , unfortunately, not. Becase any function should'nt know about types of parameters. It should works normally like in case with a standard call. ( It's very important becase I want to develop some utility that will be used to boost a lot of existing code ). So if a function should take A,B,C so at the end it should receive directly A,B,C not an array. Commented Jul 11, 2017 at 18:54
  • @Velidan Yes, the function declares one interface, and should not distinguish between way it is called. The calling code is responsible for passing its arguments in the appropriate way, and check their type if necessary. Commented Jul 11, 2017 at 19:17
  • Hint: for proper typechecking, use Array.isArray(b) not that toString magic Commented Jul 11, 2017 at 19:18

2 Answers 2

1

You can cast the value as an array first.

a(...([].concat(b)));

concat() will accept either an array or single value, and will concat it to the single array, effectively casing a single value to an array (with one value) or leaving something that is already an array alone.

const test = (a, b) => {
  console.log(a, b);
} 

const a = [1,2];
const b = 5;

test(...([].concat(a)));
test(...([].concat(b)));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. You are a genius :) So elegant and very interesting solution!
0
function a (z, c) {
  console.log('number of parameters:', arguments.length);

  console.log("z => ", z);
  console.log("c => ", c);
}

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

The arguments object is an Array-like object corresponding to the arguments passed to a function.

4 Comments

Thanks for the answer but I don't want to have any logic of typechecking/manipulation with the arguments inside a called function becase there are a lot of thirdparty "functions" so it's impossible to do that. The function call should be "standard" just like it expects.
@Velidan so you want to get this kind of information about the parameters, before it is executed?
Exactly, I want to handle this before function execution becase I want to keep the source function untouched.
@Velidan but the answer you accepted is not doing it without an execution. Check this out and tell me if it is what you are looking for: github.com/DiegoZoracKy/inspect-function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.