Javascript 我能让这个双重功能更简洁吗?
我有这个功能Javascript 我能让这个双重功能更简洁吗?,javascript,reactjs,Javascript,Reactjs,我有这个功能 const queuedAction1 = (newlyLoggedInUser) => this.callToFunction(item, newlyLoggedInUser); const queuedAction2 = (newlyLoggedInUser) => this.callToOtherFunction(item, newlyLoggedInUser); if (!someCriteria) { dispatch(queueAction(que
const queuedAction1 = (newlyLoggedInUser) => this.callToFunction(item, newlyLoggedInUser);
const queuedAction2 = (newlyLoggedInUser) => this.callToOtherFunction(item, newlyLoggedInUser);
if (!someCriteria) {
dispatch(queueAction(queuedAction1));
dispatch(queueAction(queuedAction2));
this.showLoginModal();
return;
}
queuedAction1();
queuedAction2();
调用
queuedAction
函数的顺序无关紧要,所以有没有办法让整个过程更简洁?也许只使用一个常量
?为什么不将其放入数组中
const actions = [
(newlyLoggedInUser) => this.callToFunction(item, newlyLoggedInUser),
(newlyLoggedInUser) => this.callToOtherFunction(item, newlyLoggedInUser)
];
if (!someCriteria) {
actions.forEach(action => queueAction(action));
this.showLoginModal();
return;
}
actions.forEach(action => queueAction(action));
无论何时,只要变量名为x1
,x2
,等等。这通常是一种迹象,表明您需要一个适当的数据结构,而不仅仅是一堆不相关的变量
始终尝试根据数据流对代码进行结构化,方法是将相关内容分组到一个容器中,以便于使用。为什么不将其放入一个数组中
const actions = [
(newlyLoggedInUser) => this.callToFunction(item, newlyLoggedInUser),
(newlyLoggedInUser) => this.callToOtherFunction(item, newlyLoggedInUser)
];
if (!someCriteria) {
actions.forEach(action => queueAction(action));
this.showLoginModal();
return;
}
actions.forEach(action => queueAction(action));
无论何时,只要变量名为x1
,x2
,等等。这通常是一种迹象,表明您需要一个适当的数据结构,而不仅仅是一堆不相关的变量
始终尝试根据数据流对代码进行结构化,方法是将相关内容分组到一个容器中,使它们易于使用。我投票将此问题作为离题题题来结束,因为它要求的是一个应该由我来处理的重构。我投票将此问题作为离题题题来结束,因为它要求的是一个应该经手