简体   繁体   English

javascript es6 双箭头函数

[英]javascript es6 double arrow functions

I want to better understand es6 arrow functions.我想更好地理解 es6 箭头函数。

Given the following example:给出以下示例:

    export default function applyMiddleware(...middlewares) {
      return (createStore) => (reducer, preloadedState, enhancer) => {
        // snip actual enhancer logic

        return {
            ...store,
            dispatch
        }
    }
}

Describing the above in words:用文字描述以上内容:

  1. Our exported function (applyMiddleware) takes an array parameter with spread.我们导出的函数 (applyMiddleware) 接受一个带有传播的数组参数。
  2. Then applyMiddleware returns a nameless function with a createStore parameter, which returns another nameless function this time with three parameters.然后applyMiddleware返回一个带有createStore参数的无名函数,这次返回的是另一个带有三个参数的无名函数。

So without the arrows it would look like this:所以没有箭头,它看起来像这样:

export default function applyMiddleware(...middlewares) {
  return function(createStore){
      return function(reducer,preloadedState,enhancer){
        //some logic

          return{...store,dispatch}
      }
    }
}

My questions:我的问题:

  1. Am I correct?我对么?
  2. What is the common use case/code paradigm we see here?我们在这里看到的常见用例/代码范例是什么?

The answer to your first question is more or less (see my comment).你的第一个问题的答案或多或少(见我的评论)。 The answer to your second question is that the pattern you are seeing is a combination of using a closure and currying .您的第二个问题的答案是您看到的模式是使用闭包currying的组合。 The original parameters to the exported function get gathered into an array called 'middlewares' that the returned functions close over (ie have access to).导出函数的原始参数被收集到一个名为“中间件”的数组中,返回的函数将其关闭(即可以访问)。 The function then can be called again with yet another parameter 'createStore' then another function is returned that can accept even more parameters.然后可以使用另一个参数“createStore”再次调用该函数,然后返回另一个可以接受更多参数的函数。 This allows one to partially apply the parameters.这允许部分应用参数。 For a more trivial (and perhaps more easily comprehended) example, lets take a function called 'add' that adds two numbers:对于更简单(也许更容易理解)的示例,让我们以一个名为“add”的函数来将两个数字相加:

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

Not very interesting.不是很有趣。 But lets break it up so it can take the first number and return a function that takes the second:但是让我们分解它,以便它可以采用第一个数字并返回一个采用第二个数字的函数:

let add = x => y => x + y;
let add3 = add(3);
let seven = add3(4); // 7

This may not seem like a big win for our add function, but you started with a much more reasonable real-world example.对于我们的 add 函数来说,这似乎不是一个很大的胜利,但您从一个更合理的现实世界示例开始。 Additionally, rather than currying manually it is possible (and desirable) to use a curry function that does it for you, many popular libraries (lodash, underscore, ramda) implement curry for you.此外,与手动柯里化相比,可以(并且希望)使用柯里化函数为您完成柯里化,许多流行的库(lodash、下划线、ramda)为您实现柯里化。 An example using Ramda:使用 Ramda 的示例:

let add = R.curry((x, y) => x + y);
let add3 = add(3);
let five = add3(2);
let also5 = add(3, 2);
let still5 = add(3)(2); // all are equivalent.

This answer is for those who still have some doubts in double arrow functions.这个答案是给那些对双箭头函数还有些疑问的人准备的。 Lets dig deep into it.让我们深入挖掘一下。

const doubleArrowFunc = param1 => param2 => {
   console.log('param1', param1);
   console.log('param2', param2);
}

If you call this function如果你调用这个函数

const executeFunc = doubleArrowFunc('Hello');

If you print executeFunc in console you'll get an output like this如果你在控制台打印executeFunc你会得到这样的输出

ƒ (param2) {
    console.log('param1', param1);
    console.log('param2', param2);
  }

Now this is like a half executed code.现在这就像一个执行了一半的代码。 If you wanna execute it fully, you've to do it like this如果你想完全执行它,你必须这样做

executeFunc('World');
//And the output will be 
param1 Hello
param2 World

If you want even more understanding.如果你想要更多的理解。 I can execute the same without arrow function我可以在没有箭头功能的情况下执行相同的操作

function doubleArrowFunc(param1) {
    return function innerFunction(param2) {
       console.log('param1', param1);
       console.log('param2', param2);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM