Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对javascript函数返回多个带有许多胖箭头的函数感到困惑_Javascript_Function_Ecmascript 6_Closures_Currying - Fatal编程技术网

对javascript函数返回多个带有许多胖箭头的函数感到困惑

对javascript函数返回多个带有许多胖箭头的函数感到困惑,javascript,function,ecmascript-6,closures,currying,Javascript,Function,Ecmascript 6,Closures,Currying,我的cs作业有问题。 我需要访问函数的x值,但我的代码返回的是一个空函数,而不是值 我已经在谷歌上搜索了所有的curry和closure,但是没有一个能够帮助我解决问题 const pair = (x, y) => f => f(x, y); // Do not edit this function const head = p => //Answer here console.log(head(pair(1,2))) //

我的cs作业有问题。 我需要访问函数的
x
值,但我的代码返回的是一个空函数,而不是值

我已经在谷歌上搜索了所有的curry和closure,但是没有一个能够帮助我解决问题

const pair = (x, y) => f => f(x, y);  // Do not edit this function
const head = p => //Answer here                

console.log(head(pair(1,2)))          // Do not edit this
当我尝试所有组合时,我的控制台总是返回我的
函数


函数(a,b){returna;}

您可以像这样更改
head
函数:

var pair = function(x, y) {
  return function(f) {
    return f(x, y);
  }
};

var head = function(p) {
  return function(a, b) {
    return a;
  }
};
constpair=(x,y)=>f=>f(x,y);
常数头=f=>f(a=>a)
console.log(头(对(1,2)))
我的控制台一直返回我这个

函数(a,b){返回a;}

让我们更容易阅读。在ES5中,您的代码如下所示:

var pair = function(x, y) {
  return function(f) {
    return f(x, y);
  }
};

var head = function(p) {
  return function(a, b) {
    return a;
  }
};
您需要将从
头返回的函数传递给
对(1,2)
返回的函数。因此,您需要交换调用函数的顺序:

console.log(pair(1, 2)(head()));

我不确定我是否正确理解了你的问题,但我猜你是在尝试这样做

constpair=(x,y)=>f=>f(x,y);
常数头=(x,y)=>x;

console.log((pair(1,2)(head))
然后交换每个函数中的逻辑。@Peter这里,
pair
head
之前执行。您可以添加
console.log()
进行检查。@Peter现在看到了您对问题的编辑。更新了
head
@Peter我没听懂你的话。“将函数作为参数传递”在哪里?@adiga我看了很长一段时间后没有得到f(a=>a)部分
a=>a
是一个箭头函数,就像
(x,y)=>f
。如果它太令人困惑,你可以像
(a,b)=>a那样编写它,这会增加什么?上述答案中已经提到了这两个问题。第一个问题与上述问题不同,但第二个问题是。我无法评论他们在这里提到的这一点。