如果参数是隐式提供的,你如何为函数调用提供额外的参数?

vab*_*bax 2 javascript ecmascript-6

export const SelectedChoice: (choiceProps) => { ... }

<ChoicePicker {...props}

     onRenderItem={SelectedChoice}

/>
Run Code Online (Sandbox Code Playgroud)

我有这个,参数choiceProps是隐式提供的,但现在我想向第二个参数添加一个函数,以便我可以在SelectedChoice内部调用它,我该怎么做?问题是我不知道传递给 SelectedChoice 的是什么,所以我不能在不破坏函数的情况下显式调用它。如果我知道作为 choiceProps 传递的是什么,我就不会遇到这个问题。我觉得它被称为回调函数,并且在 onRenderItem 中显式提供了参数,但我可能会误会。

小智 5

您也许能够检查arguments隐式存在于以下函数体中的对象SelectedChoice

export const SelectedChoice: (choiceProps) => {
   if(arguments.length === 1) {
      // do something if no function was passed
   }
   else if(arguments.length === 2 && typeof arguments[1] === "function") {
      // call the passed function
      arguments[1]();
   }
}
Run Code Online (Sandbox Code Playgroud)