【发布时间】:2020-06-09 20:59:46
【问题描述】:
我有两个功能:
first - takes 1 argument
secon - takes 2 arguments
然后我有第三个函数,它接收一个函数和一个值作为参数。
我无法弄清楚如何让第三个函数知道它正在接收一个参数或两个参数,具体取决于在第二个参数中传递的函数???
**在下面的示例中,当调用第三个时,它应该在代码末尾的两个调用中返回 2。
let val = 12;
let val2 = 14
let toC = 14;
// checks if val === 12 | takes 1 argument
const first = num => num === 12 ?2 :0;
// checks if val === toC | takes 2 arguments
const second = (num, check) => num === check ?2 :0;
// third function that receives a function and a val as arguments
const third = (func, num) => {
let temp = func(num);
return temp;
}
// this one works
console.log(third(first, val));
// this doesn't
console.log(third(second, (val2, toC)));
【问题讨论】:
标签: javascript function arguments