javascript - 是否可以将函数作为计算键 ES6 中的键?

标签 javascript reactjs ecmascript-6

我在 redux-connect 库中看到这段代码,我想知道是否可以将函数作为键放入 ES6 的计算键中。这是如何工作的以及 javascript 如何解释?

export const reducer = handleActions({
  [beginGlobalLoad]: state => ({
   ...state,
  loaded: false,
}),

[endGlobalLoad]: state => ({
  ...state,
  loaded: true,
})
....
}

with beginGlobalLoad 是由

创建的函数
export const beginGlobalLoad = createAction('@redux-conn/BEGIN_GLOBAL_LOAD');

我阅读了动态计算键的概念,但它没有说明将函数用作属性的键。

非常感谢您的回答

图书馆链接:https://github.com/makeomatic/redux-connect/blob/master/modules/store.js

最佳答案

没有。属性键必须是字符串或符号。如果您使用一个函数,它将像任何其他对象一样被字符串化,而这通常不是您想要的。

然而,createAction docs特别声明

createAction also returns its type when used as type in handleAction or handleActions.

他们通过 overwriting toString 实现的.

关于javascript - 是否可以将函数作为计算键 ES6 中的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45031741/

相关文章:

javascript - 如何在 Dynamic 365 CRM 客户端 Javascript 中获取用户属性?

javascript - 避免 React 中的内联函数 : How to bind functions with arguments?

javascript - Cordova Google WebView SSL 错误处理程序警报

php - 使用 AJAX 在 Javascript 中调用 PHP 函数

javascript - 是否可以在创建后设置 React 组件的样式?

javascript - react 路由器4 : Pass route props to layout component

node.js - React js 没有在 localhost :3000 上运行我的应用程序

node.js - webpack 的 NPM 启动错误

ecmascript-6 - ES6 : import specific values with namespace

javascript - 如何将多个融合图表导出到单独的图像文件