【问题标题】:How does the inner function know about this parameter?内部函数如何知道这个参数?
【发布时间】:2017-09-07 20:32:00
【问题描述】:

我正在学习 Redux-Thunk,我有一个关于 JavaScript 的一般问题。

我们如何获取dispatch函数?

function incrementAsync() {
  return dispatch => {
    setTimeout(() => {
      // Yay! Can invoke sync or async actions with `dispatch`
      dispatch(increment());
    }, 1000);
  };
}

它是像 incementAsync()(dispatch) 一样传递还是来自外部函数?

代码示例来自https://github.com/gaearon/redux-thunk

【问题讨论】:

  • var dispatch = incrementAsync(); 现在可以调用内部函数了。

标签: javascript ecmascript-6 redux redux-thunk


【解决方案1】:

内部函数接收存储方法dispatchgetState作为参数。

您的函数将被 incementAsync()(dispatch, getState) 等库使用

你不需要这样做。

【讨论】:

    【解决方案2】:

    语法param => action 用于定义一个匿名函数,该函数接受一个参数并执行一些操作。

    在你的情况下:

    var otherFunction = param => {
       console.log(param);
    };
    
    var fun = incrementAsync(); // is a function
    fun(otherFunction);
    

    【讨论】:

      猜你喜欢
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      相关资源
      最近更新 更多