Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我能让这个双重功能更简洁吗?_Javascript_Reactjs - Fatal编程技术网

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
,等等。这通常是一种迹象,表明您需要一个适当的数据结构,而不仅仅是一堆不相关的变量


始终尝试根据数据流对代码进行结构化,方法是将相关内容分组到一个容器中,使它们易于使用。

我投票将此问题作为离题题题来结束,因为它要求的是一个应该由我来处理的重构。我投票将此问题作为离题题题来结束,因为它要求的是一个应该经手