我在 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 inhandleAction
orhandleActions
.
他们通过 overwriting toString
实现的.
关于javascript - 是否可以将函数作为计算键 ES6 中的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45031741/