Confused about javascript function returning multiple functions with many fat arrow

前端 未结 3 1893
攒了一身酷
攒了一身酷 2021-01-16 14:57

I have problem with my cs homework. I need to access the x value of the function, but my codes is returning me an empty function instead of with the values

3条回答
  •  遥遥无期
    2021-01-16 15:45

    my console keeps returning me this instead

    function(a,b){return a;}

    Let's make this easier to read. In ES5, your code looks like this:

    var pair = function(x, y) {
      return function(f) {
        return f(x, y);
      }
    };
    
    var head = function(p) {
      return function(a, b) {
        return a;
      }
    };
    

    You need to pass the function returned from head to the function returned by pair(1, 2). So you need to swap which order you're calling the functions in:

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

提交回复
热议问题