Javascript function param with multiple different types of arguments

Here are 3 functions, hello1, hello2, and hello3. They take different number of parameters. The function callHello takes a callback function and an argument list, then applies the arguments to the callback function to execute the callback function. In the example below, the third call of callHello takes the function hello3 as the first parameter, and the second parameter is a list of 3 different types of data, a string, a number and an array of strings.

function hello1(s1) {
  console.log(`${s1}`);
}

function hello2(s1, s2) {
  console.log(`${s1} ${s2}`);
}

function hello3(s1, s2, s3) {
  console.log(`${s1} ${s2} ${s3}`);
}

function callHello(hello, args) {
  hello.apply(this, args);
}

callHello(hello1, ['a'])
callHello(hello2, ['a', 'bb'])
callHello(hello3, ['a', 22, ['c','c','c']])

output:

a
a bb
a 22 c,c,c

Search within Codexpedia

Custom Search

Search the entire web

Custom Search