函数作为参数转换为另一个函数JavaScript

函数作为参数转换为另一个函数JavaScript,javascript,function,Javascript,Function,当我在func2中将func1作为参数调用时,我需要访问func2中func1的参数,下面是带注释的代码: let func1 = (x, y) => x + y; let func2 = function(func1) { /* How can i get access to arguments of func1 here, when i call it like that: func2(func1(1, 2)); */ return func1; } func2(f

当我在func2中将func1作为参数调用时,我需要访问func2中func1的参数,下面是带注释的代码:

let func1 = (x, y) => x + y;

let func2 = function(func1) {
    /* How can i get access to arguments of func1 here, when i call it like that: func2(func1(1, 2)); */
    return func1;
}

func2(func1(1, 2));

您最终不得不在自己的函数中包装
func1

let func1 = (x, y) => x + y;

let func2 = function(func1) {
    return (...args) => {
        // args is now the arguments that are being passed to func1
        return func1(...args);
    }
}

// Call it like this:
func2(func1)(1, 2);

这应该能满足你的需要,因为正如易卜拉欣在评论中提到的,你正在传递它的返回值
func2(func1(1,2))
变成
func2(3)

您可以尝试将参数从func1传递到对象中并返回该对象。这将意味着重新设计应用程序的其他部分,我想,但这不应该是太多的工作。它必须使用常规函数来完成,因为箭头没有参数


您不是将函数作为参数传递,而是传递其返回值!最后一行与
funct2(3)相同!如果你想知道论点,你需要做一些类似于func2(func1,1,2)的事情。我不相信你不能按照你想的方式做你想做的事情。你能更明确地说明你的最终目标吗?这看起来比我的答案好
const func1 = function(x, y) {
  const args = arguments
  return {
    args: args,
    main: x + y,
  }
}

const func2 = function(func1) {
    const otherArguments = func1.args
    console.log(otherArguments)
    return func1.main;
}

func2(func1(1, 2));